How to Randomly Swap Similar PowerPoint Shapes with VBA
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
Click 'Insert' in the top menu and select 'Module' to create a blank workspace for your code.
Write a macro that establishes two arrays of Top and Left coordinate pairs: one for your 'TextBox' names and one for your 'Photo' names.
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.
Randomize Shape Visibility Instead of Moving Them
Instead of changing shape coordinates, stack all images in the same location and use a macro to randomly display one at a time.
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. Open Your Macro-Enabled File: Launch WPS Presentation and open your .pptm presentation file.
- 2. Access the Developer Tab: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon.
- 3. Insert Your Code: Right-click in the project explorer, select Insert > Module, and paste your coordinate-swapping macro code.
- 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.

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.




