VBA & Macro Problems

How to Randomly Swap Similar PowerPoint Shapes with VBA

John WilsonJohn Wilson Oct 26, 2026 868 views

Question details

The user wants to use a VBA macro to randomly rearrange specific groups of shapes (such as 26 text boxes and 26 photos) while keeping each group in its designated area.

How to Randomly Swap Similar PowerPoint Shapes with VBA
Product
PowerPoint
Device & OS
not provided
Scenario
Creating an interactive slide where alphabet letters stay at the top and corresponding images stay at the bottom, but both groups are shuffled randomly.
Observed behavior
Standard shape-swapping macros randomly exchange all shapes across the entire slide, causing text boxes and images to mix and leave their designated top and bottom regions.
Before you start

Ensure your presentation is saved in a Macro-Enabled format (.pptm) and that the Developer tab is enabled in your ribbon to access the VBA editor.

Solution 1Recommended

Use VBA Arrays to Randomize Coordinates within Shape Groups

This solution creates specific location arrays for each shape group, allowing you to move items randomly within their designated area without swapping across different groups.

Instead of swapping shapes randomly across the whole slide, you can define specific safe zones. By creating an array of valid X and Y coordinates for the text boxes and a separate array for the photos, you ensure that letters stay at the top and images stay at the bottom.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a blank workspace for your code.

3
Define Your Coordinate Arrays

Write a macro that establishes two arrays of Top and Left coordinate pairs: one for your 'TextBox' names and one for your 'Photo' names.

4
Assign Random Positions

Loop through the shapes on your slide using a For Each statement. Check if the shape name begins with 'TextBox' or 'Photo', then randomly assign an available coordinate pair from the corresponding array.

Macro Naming Conventions: Ensure your shapes are strictly named using the Selection Pane (e.g., TextBox1 to TextBox26) so the VBA code can correctly identify and group them.

Run Macros and Swap Shapes Effortlessly in WPS Presentation

WPS Presentation provides excellent compatibility with Microsoft PowerPoint files and supports running VBA macros to automate complex tasks like randomizing shape positions.

  1. 1. Open Your Macro-Enabled File: Launch WPS Presentation and open your .pptm presentation file.
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon.
  3. 3. Insert Your Code: Right-click in the project explorer, select Insert > Module, and paste your coordinate-swapping macro code.
  4. 4. Run the Macro: Return to your slide view, go to Developer > Macros, select your newly pasted macro, and click 'Run' to see the shapes randomize.
Highly compatible with Microsoft Office .pptm and .ppt filesIntuitive user interface with a built-in Developer tab for VBA macrosLightweight, fast-loading, and free to downloadEasy management of shape names and selection panes
microsoft office alternative - wps office

Frequently Asked Questions

How do I find or change the exact name of a shape in PowerPoint for my VBA code?

You can find and rename shapes using the Selection Pane. Go to the Home tab, click on Select in the Editing group, and choose Selection Pane. Double-click any shape name in the list to rename it.

Why does my VBA macro generate the same random sequence of shapes every time I open the presentation?

VBA's random number generator needs to be initialized. Add the 'Randomize' statement at the very beginning of your macro to ensure a unique sequence of numbers is generated each time the macro runs.

Can I undo the movement of shapes after running a VBA macro?

No, running a VBA macro clears the undo history stack in Office applications. It is highly recommended to save your presentation or create a backup copy before executing macros that manipulate shapes.