How do I use VBA code in PowerPoint
You'll find lots of useful snippets of VB or VBA code on the internet and in newsgroups, but unless you know how to get the code into PowerPoint, you can't very well use them. Here's a quick tutorial on the subject.
You may prefer to use this mini-site I created for a VB Programming session I did at PPTLive
Set security options
Before you can run VBA code, you need to set PowerPoint's security options to permit macros to run. The following steps set things up so that when you open a presentation that contains macros, PowerPoint asks whether or not to disable them.
PowerPoint 97-2003
Open a presentation or start a new one.
Choose

On the Security Level tab of the Security dialog box, select Medium

Click
PowerPoint 2007
Click the Office button (the great big circular thing, upper left of the PPT screen)

Click

Click

Click

Whew. Close any open dialog boxes.
PowerPoint Mac 98-2004
You don't need to do anything about macro security in order to run macros, but it's a good idea to make sure that it's enabled. Choose PowerPoint, Preferences, General and use the
PowerPoint Mac 2008
Mac PowerPoint 2008 doesn't support macros. You're perfectly safe. And alas, perfectly unable to use macros. Sorry.
Start the VBA editor
The VBA editor (also called the Integrated Development Environment ... IDE to its friends) is where you'll work with VBA/macro code in PowerPoint. To start the VBA editor/IDE:
PowerPoint 97-2003
- Press ALT+F11 to start PowerPoint's VBA editor.
- Or choose
View, Toolbars, Visual Basic to get shortcut buttons to the editor and for running macros. - Or choose
Tools, Macro, Visual Basic Editor to start the editor.
PowerPoint 2007
- Press ALT+F11 to start the VBA editor.
- Or click the Office button, choose
PowerPoint Options , click Popular and put a check next toShow Developer tab in Ribbon . Close the options dialog box, click theDeveloper tab then clickVisual Basic to start the editor.
Mac PowerPoint
Use
Add code
In the VBA editor, make sure that your presentation is highlighted in the left-hand pane.
Choose
If your code snippet already starts with "Sub XXX()" and ends with "End Sub", simply click in the new module you just inserted and paste in the code. Otherwise, you'll have to type in "Sub XXX" (where XXX is the name you want to give the subroutine (aka "macro"). When you press Enter, PowerPoint adds the parentheses and End Sub for you automatically. Then position the cursor between the Sub XXX and End Sub lines and paste in your code.
To make sure there are no serious syntax problems with the code, choose
Now click the Run button (a right-facing arrowhead icon), choose
Put it to work
Once your code's working properly, you can run it directly from within PowerPoint without having to start the VBA editor.
Choose
You can also view and run macros from other open presentations; choose the presentation where your macro is stored from the Macros In dropdown listbox.
You can also use
Note: Code that compiles may still not do what you expect it to do when you run it, or it may still produce errors. If it errors out, VBA will show you a message box that lets you either End the code or Debug it. If you choose Debug, you'll be returned to the VBA editor with the problem line highlighted in yellow so you can correct the problem.
If you want to run your code without having to load the PowerPoint file it's in every time, see Create an ADD-IN with TOOLBARS that run macros
Housekeeping hints
As you accumulate additional code snippets, you'll want to figure out where you'll store them.
You could print them, but then you'd have to re-type later (and possibly introduce errors that keep the code from running correctly).
Instead, when you see some useful-looking code, select it, copy it to the clipboard, then start Notepad (Windows) or TextEdit (Mac) and paste the code into it. Then save the file, possibly to a special folder you've created just for VB/VBA code snippets. Getting the code into PowerPoint is then a simple matter of opening the Notepad file, selecting the text and copying it into PowerPoint.
If the code turns red when you paste it into a module, the likely cause is unwanted linebreaks or other extra characters (invisible) copied from the web browser or newsgroup reading program. Delete everything between line breaks and get rid of spaces before indented items to correct this problem.

