Macros in PowerPoint: Full Tutorial and How to Write VBA Code for a “Swap Multiple Shapes” Macro
In this tutorial, you’ll learn how to set up macros in PowerPoint, and you’ll get practice writing VBA code for your first macro.
Macros in PowerPoint are useful for tasks such as performing tricky alignments, fitting shapes within tables, and using Drawing Guides, rather than physical lines, to distribute shapes.
Before you start using macros or writing your own VBA code, you must understand the fundamentals of PowerPoint: features like the Quick Access Toolbar, the Slide Master, Tables, and how to duplicate a shape.
It’s counterproductive to “automate” slides and presentations unless you first understand the key PowerPoint commands and shortcuts.
In this tutorial, we’ll walk you through how to create your first PowerPoint macro, which you can use to swap the positions of multiple shapes.
This code is simple, but it is also very useful because it typically takes several keyboard shortcuts and mouse drags to swap shapes manually, so an automated solution is a clear win.
And amazingly, there is no built-in way to do this in the standard version of PowerPoint.
Video Table of Contents:
0:58: Why Macros Are Useful in PowerPoint
2:44: PowerPoint Macro Demo
6:27: Lesson Overview
6:40: VBA in Excel vs. PowerPoint
10:09: Simple “Shape Swap” Macro
18:29: Macro to Swap Multiple Shapes
25:29: Recap and Summary
Files & Resources:
PowerPoint Macros and VBA in Excel vs. PowerPoint
Before jumping into the code, it’s worth asking two key questions:
- What are the advantages and disadvantages of VBA and macros in Excel vs. PowerPoint?
- What are good vs. bad use cases for macros in PowerPoint? In other words, what is the most effective way to spend your time automating your presentations?
On the first question, VBA in Excel is simpler to set up and use for quick macros.
Excel has a macro recorder, so you can record your actions in a spreadsheet, review them in the VBA Editor, and modify the code to do what you want.
Also, assigning keyboard shortcuts to your macros is easy because you always select a keyboard shortcut when you record actions in the macro recorder.
By contrast, PowerPoint macros are more difficult to set up but are arguably more powerful.
Most Excel macros function based on a selected range of cells in a single spreadsheet and automate processes like color-coding the cells or changing the decimal places.
That’s nice, but PowerPoint macros often change the entire presentation, including on normal slides and templates in the Slide Master.
Also, PowerPoint macros do not break the “Undo” command, so you can press Ctrl + Z (or ⌘ + Z on Mac) repeatedly, and it will work correctly with all macros.
But in Excel, macros break the Undo and Redo commands unless you build a workaround into your code, which can get very complicated.
Here’s a summary of VBA in Excel vs. PowerPoint:
Returning to the second question above – good vs. bad use cases for macros in PowerPoint – focus on macros that are simple to code and that automate actions you repeat a lot.
For example, swapping shapes is quite simple to code (5-10 minutes), and it saves you time because it’s cumbersome to swap shape positions manually. Plus, it’s a common task when editing presentations.
On the other hand, it’s silly to write a macro that “centers” a shape vertically and horizontally on a slide because the “Align Center” and “Align Middle” commands already do this, and it’s not especially common to center single shapes on a slide in corporate presentations.
Something like the Table of Contents macro in our full macro package, which is based on the Slide Master and custom layouts, is in the “maybe” category.
It saves you time, but it’s also complicated to code and test, and it doesn’t work 100% perfectly in all cases.
Plus, you might only add the Table of Contents when you’re finished with a presentation, so this macro may be less useful than simpler shape manipulation commands.
Your First PowerPoint Macro: “Swap Shapes”
To start writing your first macro, go to the “Trust Center” in PowerPoint (Alt, T, O in the PC version or ⌘ + , on Mac) and make sure the program will let you run macros:
Use one of the settings above (the screens will look slightly different on the Mac) and make sure the “Developer Toolbar” in the ribbon menu is visible by going to “Customize Ribbon” within the Options menu:
Once you’ve done this, open the VBA Editor with Alt, L, V on the PC (there is no Mac shortcut, so navigate there manually) and insert a “module” and a “subroutine” to write a new macro:
You can call the new module “SwapShapes” and add a new subroutine with the same name on the right side of the screen:
After you type “Sub SwapShapes()” VBA will automatically insert the “End Sub” at the end to indicate that your macro ends there.
With simple macros, you usually want to work with the shapes, slides, or text the user has selected.
That’s how this “Swap Shapes” macro will work: it will assume that the user has selected the shapes they want to swap, and then it will change their positions.
First, you need to make sure the user has selected shapes, and if so, that they’ve selected 2 shapes rather than 1, 10, or 50 shapes:
“IF” statements are the building blocks of all programming languages, including VBA.
They let you check conditions, such as the selection consisting of 2 shapes, and they take actions based on whether these conditions are true or false.
The ActiveWindow.Selection object in VBA contains whatever the user has selected (shapes, slides, text, or nothing at all), and it has “properties” for things like the selection type and the number of objects selected.
You can use the “IF” statements with ActiveWindow.Selection to check for these conditions.
If you’re unsure of an object’s properties, you can start typing its name followed by a “.” so that VBA displays a list of options.
The “=” operator is used for both assignments and equality checks in VBA, which is a bit confusing. But if it’s part of an “IF” statement, as it is here, it’s an equality check.
The MsgBox command is useful for testing the code as you move along and ensuring the “IF” statements work.
Next, you need to save the first shape’s Top and Left positions and put them in “variables” that you can refer to later.
Here’s the code:
The “=” signs in the main part of the code are assignment operators because they’re not within “IF” statements.
So, they SET one shape’s Left and Top coordinates to the other shape’s Left and Top coordinates.
Again, it is confusing how “=” can check for equality in VBA and set the value of a variable; there is no easy answer other than “continued practice and exposure.”
The ActiveWindow.ShapeRange(1) part means: “Take the first shape the user has selected on the current slide.”
You can use ActiveWindow.ShapeRange(2) to refer to the second shape, which takes us into the next part: setting the first shape’s Top and Left positions to those of the second shape.
If you stopped here, you’d have a problem because you’ve now lost the first shape’s original Top and Left positions.
This is why you saved them in the tempLeft and tempTop variables: by saving these original positions in variables, you can now use them to change the second shape’s position.
This code properly swaps the positions of two shapes.
However, you can make it more efficient by using a “With” statement, which also exists in Excel VBA, to remove the need to type ActiveWindow.Selection:
When you type the “With ActiveWindow.Selection” line, anything that starts with a “.” between that and the “End With” is assumed to be part of ActiveWindow.Selection.
So, VBA “translates” a line like this:
tempLeft = .ShapeRange(1).Left
tempLeft = ActiveWindow.Selection.ShapeRange(1).Left
You can now go into PowerPoint and test this macro with different shapes on the reference slides.
To do this, use the Alt, L, PM shortcut in the PC version (no Mac equivalent, so navigate to Developer in the ribbon menu and click on Macros), select “SwapShapes” and click “Run”:
As a final step, you can save this file as a macro-enabled presentation in the .pptm format:
By doing this, you’ll ensure that whoever opens the file next can still use this macro.
The Limitations of Macros in PowerPoint
This simple exercise, while useful, also reveals a few issues with macros in PowerPoint:
1) Keyboard Shortcuts – There is no easy way to assign keyboard shortcuts to macros; you must activate them through the “Macros” menu in the Developer Toolbar.
2) Macro-Enabled Files – While you can save macros with the above method, it is not ideal for sharing them or making them usable across different presentations.
3) Code Constraints – It’s simple to write code that handles only 2 shapes, but it’s not immediately obvious how to extend it to manage multiple shapes.
We could fix these issues now or explore other enhancements, but the first two points above are surprisingly complicated to solve.
So, we’ll focus on point #3 and extend this macro to make it swap multiple shapes:
An Extension to Macros in PowerPoint: “Swap Multiple Shapes”
You can extend this macro to swap multiple shapes with a few simple changes.
Start by changing the variable declarations and error checks at the top.
When the user selects multiple shapes, you need to save the first shape’s positions, and you need to create a “counter variable” that tracks the shape # you’re currently on.
For example, if the user has selected 10 shapes, you need to know if you’re currently on shape #1, #2, #3, or #4-10 as you move through the selection and change each shape’s positions.
Also, you need to make sure the user has selected more than 1 shape – not necessarily just 2 shapes:
Next, you need to “loop” through all the shapes the user has selected with a “For” statement.
So, if the user has selected 10 shapes, you need to move from shape #1 through shape #10 and change the position of each shape.
You can start by typing the syntax for this “For” loop:
For an example of how this works, continue assuming that the user has selected 10 shapes.
In this case, you should loop through shapes #1 – #9 and set each shape’s Left and Top positions to the next shape’s Left and Top positions.
So, Shape #1 Top should become Shape #2 Top, and Shape #2 Top should become Shape #3 Top.
When you reach shape #10, you should set its Top and Left positions to those of the first shape.
This means you need to save shape #1’s Top and Left positions before starting this loop.
You can start by handling the case for shapes #1 – 9, or “everything before the final shape”:
As the next step, you can add a special case to save the first shape’s position before the “For” loop and set the last shape’s position equal to the first shape’s:
You can now test these changes on the reference slides and verify that this macro “rotates” multiple shapes:
Activate the macro enough times, and the shapes will return to their original positions.
Macros in PowerPoint: Beyond the Surface-Level Detail
If you’ve followed the steps above, you should have a “Multi-Shape Swap” macro you can use to rearrange your slides.
But this tutorial just scratches the surface; it represents ~30 minutes out of the 12-13 hours of VBA training in our full PowerPoint Pro course.
You can do far more with macros and VBA than simple shape manipulation – as shown in the video above, you can manipulate tables, combined table/shape designs, and even the Language properties of entire presentations.
And you can automate the alignment, distribution, and formatting processes in many ways, including the clever use of Drawing Guides.
You can see the full set of macros in the course below:
You’ll gain access to the full package and all the detailed tutorials as soon as you sign up for the PowerPoint Pro course: