Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

Proud member of

PPTools

FREE PPTools add-ins like THOR - The Hammer!, ImageInsert, Narrator, SelectionManager and more

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

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:

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:

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:

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.


Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2024 Stephen Rindsberg dba PPTools and the MS PowerPoint MVP team.
You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

How do I use VBA code in PowerPoint
http://www.pptfaq.com/FAQ00033_How_do_I_use_VBA_code_in_PowerPoint.htm
Last update 15 August, 2024
Created: