How do I use VBA code in PowerPoint
I found this VBA code that's supposed to do what I want. NOW what?
You'll find lots of useful snippets of VB or VBA (also called "macro") code on the internet, but unless you know how to get the code into PowerPoint, you can't very well use it. Here's a quick tutorial on the subject.
Note: If you're using PowerPoint on the web or a mobile device (iPhone, iPad, Android), either stop reading now or switch to a Windows or Mac desktop version before continuing. VBA only works in desktop versions of PowerPoint.
AnotherNote: Most of the following is written for Windows PowerPoint users. Windows keyboard shortcuts won't work on Mac, but we'll explain how you can get into the Mac VBA editor, and from there, it's pretty much the same.
VERY USEFUL TIP
Start a new blank presentation and add your VBA code to it rather than adding it to the presentation you intend the VBA to work on. Save the file as a PowerPoint Macro-Enabled Presentation (*.pptm).
That'll let you run the code on any open presentation you like. You won't have to add your VBA code to every file you want to run it on. All you need to do is:
- Open the PPTM file that contains your VBA
- Open the file or files you want to run your VBA on
- Make sure that the file you want to run the VBA on is the currently active file
- Press ALT+F8, choose Macro in: All open presentations then double-click the name of the macro you want to run.
Think of this PPTM as your VBA code library. You can store various handy macros in it and, as long as the PPTM file is open, run you code against any other open presentation.
You might like to view this mini-site I created for a VB Programming session I did at Presentation Summit. The setup info is outdated, but the how-to-code examples are still valid.
Or this tutorial in PDF format. Same caveat applies.
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, 2016, 2019, 2021, 365
You'll see slight differences between the PowerPoint 2007 screens here and later versions, but the steps are basically the same.
PowerPoint 2007:
Click the Office button (the great big circular thing, upper left of the PPT screen)
PowerPoint 2010/2013/2016/2019/2021/365:
- Choose File
- Click 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.
PowerPoint Mac 2011/2016 and anon
VBA disappeared from Mac PowerPoint 2018, came back in Mac PowerPoint 2011, and is still there in 365/2016/2019; badly crippled at first but as of late 2018, a real contender again. We still have to do some coding in Mac PPT 2011 (or in Windows PPT) then test/debug in 2016, though, because:
- We can't yet create or edit forms
- There are no handy apps for editing RibbonX code (again, over to the PC for that unless you want to do the XML editing in a text editor).
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.
Any Windows PowerPoint version
Press ALT+F11 to start the IDE.
Mac PowerPoint pre-2008
Use View, Toolbars, Visual Basic then click the Visual Basic Editor button on that toolbar.
Mac PowerPoint 2011/2016/later
Choose Tools | Macro | Visual Basic Editor to start the IDE.
Add code
In the IDE, 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 within a project.
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 ... sort of explaining, though in a geeky way that doesn't always make sense) 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.
Once it compiles successfully, be sure to leave the IDE and save the file (as a PPTM file) from within PowerPoint. After you've saved it once, you can save it again from within the IDE using CTRL+S or File | Save from the menu bar.
Now choose Run | Run Sub/User Form from the menu bar or press F5 to run your code and verify that it works as expected.
Note: If you notice that your code didn't do what you wanted to your presentation, or did something that you DIDN'T want, press CTRL+Z while viewing the presentation. That'll undo everything your code just did.
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.
Press ALT+F8 or choose Tools | Macros | Macros to get a list of available macros ( = VBA subroutines, remember?) in the current presentation in the Macro dialog box.
Highlight the macro you want to run and click Run (or simply doubleclick it).
You can also view and run macros from other open presentations. Choose the presentation where your macro is stored, or All open presentations from the Macros In dropdown listbox. This is how you'd run the macro in one presentation on the slides in another presentation.
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 store them where you can access them quickly and easily.
Our suggestion: Since you're already putting your code in a separate PPTM file, you can use this same file as a kind of common code repository. Store all your code in it. You can even add more modules (ALT+ I M or from the Insert menu) to organize your code as you wish, then run it from the Macros menu (ALT + F8) whenever your private code stash file is open.