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.
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 2007, 2010, 2013 and 2016
You'll see slight differences between the PowerPoint 2007 screens here and later versions, but the steps are basically the same.
Click the Office button (the great big circular thing, upper left of the PPT screen)
- Choose File
- Click PowerPoint options... at the bottom of the menu that appears
- Click Trust Center on the left of the PowerPoint Options dialog box, then click Trust Center Settings on right.
- Click Macro Settings on the left of the dialog then choose Disable all macros with notification.
Whew. Close any open dialog boxes.
- Open a presentation or start a new one.
- Choose Tools, Macro, Security
- On the Security Level tab of the Security dialog box, select Medium
- Click OK
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 Enable macro virus protection checkbox to turn the macro warning on.
PowerPoint Mac 2008
Mac PowerPoint 2008 doesn't support VBA. You're perfectly safe. And alas, perfectly unable to use VBA. Sorry.
PowerPoint Mac 2011/2016
VBA came back in Mac PowerPoint 2011. And is still there in 2016, though badly crippled. If you want to do any serious VBA coding, do it in 2011 (or in Windows PPT) then test/debug in 2016.
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:
- Press ALT+F11 to start the VBA editor.
- Or click the Office button, choose PowerPoint Options, click Popular and put a check next to Show Developer tab in Ribbon. Close the options dialog box, click the Developer tab then click Visual Basic to start the editor.
- Press ALT+F11 to start the VBA editor.
- Or choose File | Options | Customize Ribbon and put a checkmark next to Developer in the listbox under Customize Ribbon. Close the options dialog box, click the Developer tab then click Visual Basic to start the editor.
- 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.
Mac PowerPoint pre-2008
Use View, Toolbars, Visual Basic then click the Visual Basic Editor button on that toolbar.
Mac PowerPoint 2011
Choose Tools | Macro | Visual Basic Editor to start the Visual Basic Editor.
In the VBA editor, make sure that your presentation is highlighted in the left-hand pane.
Choose Insert, Module from the menu bar to insert a new code module into your project (project = presentation in VBAspeak). Modules are one of the several "containers" that can hold VBA code.
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 Debug, Compile from the menu bar. If there's a problem, you'll see a message explaining (well ... explaining in a geeky, obtuse way that usually won't make any sense to you) what VBA doesn't like about the code. Click OK and the problem line will be highlighted for you. Fix it and compile again until you get no error messages.
Now click the Run button (a right-facing arrowhead icon), choose Run, Run Sub/User Form from the menu bar or press F5 to run your code.
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 Tools, Macros, Macros to get a list of available macros ( = subroutines, remember?) in the current presentation. Highlight the one you want to run and click Run (or simply doubleclick the one you want).
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 Tools, Customize to create your own toolbar and assign a macro to it. This won't work reliably unless the PPT file that contains the macros is open in PPT at the same time, but it's a handy way to invoke a macro with one click rather than having to chase through the Tools, Macros menu to get there.
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
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.