You insert VBA code in PowerPoint by turning on the Developer tab, opening the Visual Basic editor, adding a module to your presentation, pasting the code there, and then saving the file as a macro-enabled presentation (.pptm). You cannot run or store macros in a standard .pptx file, so the save step matters as much as the code itself. Nothing extra needs installing: the VBA editor ships with every desktop copy of PowerPoint for Windows and Mac, though the exact steps differ between the two. The steps below are written for PowerPoint on Windows; Mac differences are noted where they matter.

What You'll Need

  • A desktop version of PowerPoint for Windows or Mac. PowerPoint for the web has no VBA editor, and mobile apps (iPhone, iPad, Android) cannot open or run macros at all.
  • The VBA code you want to run, either copied from a source you trust or written yourself. Treat any code from an unknown source like an email attachment from a stranger.
  • A sample presentation you do not mind testing on, since macros can change slides, shapes and content in ways that are hard to undo.
  • The ability to change PowerPoint's Trust Center settings if macros are blocked on your machine, which may require administrator rights in a managed work environment.
  • Ideally, the file saved somewhere that supports macros, such as a local drive, a network share or OneDrive. Some syncing and mail systems strip or block macros, so keep a local copy as a backup.

Step-by-Step

  1. Save a copy of your presentation first. Use File > Save As and give it a clear name. Macros can delete slides, rename shapes or change formatting, and having a clean original means you can always start again.
  2. Show the Developer tab. Open File > Options > Customize Ribbon. In the list of main tabs on the right, tick Developer, then click OK. On Mac, use PowerPoint > Preferences, look for the Ribbon or View settings, and enable the Developer tab there. If you already see a Developer tab on the ribbon, skip this step. Labels can differ slightly between versions and regions, so look for the closest match if a name is not exactly as written here.
  3. Open the VBA editor. Click the Developer tab and then click Visual Basic, or press Alt+F11 on Windows. A separate editor window opens, with a Project pane on the left listing your open presentations. On Mac, the shortcut is different, so use the button on the ribbon instead.
  4. Find your presentation in the Project pane. It appears under a heading such as VBAProject (YourFileName). If the Project pane is hidden, use View > Project Explorer inside the editor. Expanding the tree shows folders for Microsoft PowerPoint Objects, Modules, Class Modules and Forms.
  5. Insert a standard module. Click your presentation in the Project pane so it is selected, then choose Insert > Module from the editor's menu bar. A blank code window opens on the right with a name like Module1. Standard modules are where general-purpose code belongs. Code that must respond to presentation events goes in a slide or presentation object instead, but most copy-and-paste macros use a standard module.
  6. Paste the code into the blank module. Click inside the code window and paste with Ctrl+V, or type the code. Leave the Option lines that may already be at the top of the module if your code depends on them. If you typed the code rather than pasting it, check that no lines are cut off.
  7. Test it from the editor or the ribbon. To run a macro immediately, place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. Alternatively, close the editor, return to PowerPoint, click Developer > Macros, select the macro from the list and click Run. Never run an unfamiliar macro on a file you have not backed up.
  8. Save as a macro-enabled presentation. In PowerPoint, use File > Save As and choose PowerPoint Macro-Enabled Presentation (.pptm) from the file type list. If you leave it as .pptx, PowerPoint shows a warning that the macros cannot be saved in that format and your code will be lost. Click Yes if prompted to keep the macro-enabled format.
  9. Reopen the file and confirm it works. Close the presentation, open it again, and check that the macro still appears under Developer > Macros. If the file opens with a yellow security bar saying macros are disabled, click the Enable Content button on that bar. If you trust the file and want to avoid the prompt every time, you may need to add its folder to PowerPoint's trusted locations.
  10. Run the macro at the right time. Macros assigned to shapes run when the shape is clicked: right-click the shape, choose an option like Assign Macro, and pick your procedure. Macros you simply want on demand can be added to the Quick Access Toolbar from File > Options > Quick Access Toolbar, choosing Macros from the dropdown.

Common Mistakes

  • Saving as .pptx and losing the code. The editor accepts your code, but PowerPoint cannot store it in a standard presentation, so always switch to .pptm.
  • Pasting into the wrong place. Code dropped into ThisPresentation or a slide object may fail to appear in the macro list. If you cannot see your macro, insert a standard module and paste it there.
  • Copying code from a web page with formatting. Smart quotes, line numbers or wrapped lines break VBA. Paste as plain text or retype the problem lines.
  • Forgetting that many macros expect specific shape or slide names. Code written for one deck often fails in another; read any notes that came with it.
  • Ignoring macro security prompts. If macros stay disabled, the code is present but will not run, which looks like a broken file.
  • Sharing the .pptm by email or chat. Many services block or strip macro-enabled files, so the recipient may never receive the code.

Tips

  • Keep a small test deck with a couple of shapes and slides so you can try code safely before using it in real work.
  • Press F8 in the editor to step through code one line at a time when something behaves unexpectedly.
  • Print or export your code to a text file as a backup; recovering lost VBA from a damaged PowerPoint file is difficult.
  • Comment your code with an apostrophe at the start of a line so future you knows what each section does.
  • Test on the computer that will actually run it. Mac and Windows PowerPoint share most VBA but not all of it, and some features behave differently.

Troubleshooting

If the Developer tab does not appear after ticking it, close and reopen PowerPoint, and check whether your organisation locks the ribbon layout. On a work machine the setting may be greyed out.

If Alt+F11 does nothing, open the Developer tab and click Visual Basic directly, or check for another program intercepting the shortcut.

If PowerPoint refuses to run macros, look for the security bar and click Enable Content. If it is absent, open File > Options > Trust Center > Trust Center Settings and review Macro Settings. Choose the option that prompts before enabling, and only enable macros in files you created or received from a source you trust. Do not disable all security warnings permanently to silence the prompt.

If your macro runs but nothing visible happens, it may be working on a shape or slide that does not exist in this deck, or it may need a selection made first. Run it with F8 to see where it stops.

If the file opens with broken code or errors, close without saving, restore the backup, and paste the code again more carefully. If PowerPoint itself crashes on opening, open a blank deck and use the editor's File > Import to pull in code from a clean copy, or recover from your saved text backup.

Frequently Asked Questions

Can I add VBA in PowerPoint for the web or on a phone?

No. The browser version and the mobile apps cannot open the VBA editor or run macros. You need desktop PowerPoint.

Why do my macros disappear when I save?

The file is almost certainly saved as .pptx, which cannot store macros. Use Save As and pick .pptm.

Are macros safe?

They can be. Macro viruses exist, so only enable content in files you made or received from someone you trust, and keep macro security at the prompt setting rather than disabling warnings.

Can I run a macro automatically when the slide show opens?

You can trigger code from presentation events in specific objects, but this is advanced and depends on the code you use. Most users run macros manually or by clicking a shape.

Does VBA code work the same on Mac and Windows?

Mostly, but not entirely. Some features differ or are missing on Mac, so test on the target platform before relying on it.