How to Create a VBA Back Button for Multiple Branching Paths in PowerPoint
Question details
Create a dynamic back button that remembers the slide history in a branching presentation.

- Product
- PowerPoint
- Device & OS
- not provided
- Scenario
- Designing an interactive presentation where multiple different slides hyper-link to the same destination slide, requiring a back button to return to the specific slide the user just came from.
- Observed behavior
- Standard PowerPoint hyperlinks and "Last Slide Viewed" actions fail or loop incorrectly when multiple paths lead to a single destination, leaving the user unable to return to their original starting slide.
Ensure your presentation is saved as a PowerPoint Macro-Enabled Presentation (.pptm) to retain your VBA code, and enable the Developer tab in your ribbon to access the Visual Basic Editor.
Create a VBA Array Stack to Track Visited Slides
Implement a VBA macro using an array to record your slide history and a custom procedure to dynamically return to the last viewed slide.
To solve the branching path issue, you need a way to log the chronological sequence of slides viewed. By declaring a public array as a "stack," you can push the current slide index to the stack every time the slide changes. A corresponding "GoBack" macro will then pop the last slide off the stack and navigate to it.
Press Alt + F11 to open the Visual Basic Editor. Go to Insert > Module to create a new standard module for your code.
At the very top of your module, declare your array and a counter variable. For example: `Public aStack() As Long` and `Public StackCount As Integer`. Ensure these are declared outside any Sub or Function.
Add an `OnSlideShowPageChange` event subroutine. Inside this procedure, increase your StackCount and assign the current `ActivePresentation.SlideShowWindow.View.Slide.SlideIndex` to `aStack(StackCount)`.
Write a `GoBack` subroutine. It should check if the StackCount is greater than 1, navigate to `aStack(StackCount - 1)` using `SlideShowWindow.View.GotoSlide`, and then decrease the StackCount by 1.
Close the VBA editor and return to your slide. Insert a shape, right-click it, select 'Action Settings', click 'Run macro', and choose your `GoBack` procedure.

Build Interactive Presentations with WPS Office
WPS Presentation fully supports advanced macros and VBA scripting, allowing you to easily create complex branching paths and custom dynamic navigation buttons just like you would in Microsoft PowerPoint.
- 1. Enable the Developer Tab: Open WPS Presentation, go to the Options menu, and customize the ribbon to display the Developer tab.
- 2. Launch the VBA Editor: Click the Developer tab and select 'Visual Basic Editor' to begin writing your slide tracking script.
- 3. Insert the Tracking Code: Create a new Module and insert the public array and `OnSlideShowPageChange` tracking code exactly as you would in standard VBA.
- 4. Assign the Macro: Insert a navigation shape on your slide, right-click to access Action Settings, and assign your custom 'GoBack' macro to execute on click.

Frequently Asked Questions
Why doesn't the standard 'Previous Slide' action work for branching presentations?
The standard 'Previous Slide' action simply navigates one slide backward in the sequential order of the slide deck, rather than remembering the actual chronological history of which slides the user clicked through.
What does the 'Invalid outside procedure' compile error mean in VBA?
This error occurs when you place executable commands or logic directly in the module body. Only variable declarations (like Public or Dim) can exist outside of a Sub, Function, or Property procedure block.
How do I trigger a VBA macro automatically when a slide changes?
You can use the built-in `OnSlideShowPageChange` event handler. Any code placed inside a subroutine with this exact name will automatically execute every time the presentation navigates to a new slide during slideshow mode.
Will my VBA back button work if I share the presentation with others?
Yes, but only if they open the file in macro-enabled software (like PowerPoint or WPS Office) and explicitly click 'Enable Macros' when prompted by the security warning. If macros are disabled, the custom back button will not function.




