How do I use VBA code in PowerPoint
I found this VBA code that's supposed to so 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 explain how you can get into the Mac VBA editor, and from there, it's pretty much the same.
Hint: 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. That'll let you run the code on any open presentation you like, and will prevent you from having to save your presentations as PPTM (PPT with Macro) files, which can trigger security warnings for you and other users.
You may prefer to use this mini-site I created for a VB Programming session I did at PPTLive. 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
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 ... 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.
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 ( = subroutines, remember?) in the current presentation in the Macro dialog box.
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, 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.