Run Reusable VBA Code from PowerPoint Slide Buttons
Question details
The user wants to reuse VBA code across many PowerPoint slides to show or hide figures with toggle buttons, without placing separate event code in every slide object.

- Product
- Microsoft PowerPoint desktop with VBA
- Device & OS
- not provided
- Scenario
- VBA code imported with VBProject.VBComponents.Import becomes a standard module. Slide Show buttons need to call that shared code and act on the current slide.
- Observed behavior
- The existing TB1_Click event works when placed in the Slide1 object, but importing the code into a standard module does not connect it to the slide control.
Open the presentation in a desktop version of PowerPoint that supports VBA and save it as a PowerPoint Macro-Enabled Presentation (.pptm). Check that each target slide contains the figure you want to toggle, and enable macros only if you trust the presentation and its code.
Connect slide action buttons to one reusable VBA procedure
Use a normal shape with a Run Macro action instead of duplicating an ActiveX control's click event on every slide.
A standard module is the correct place for reusable VBA procedures. Importing a module does not automatically connect a procedure such as TB1_Click to a slide control's event.
PowerPoint can pass the clicked shape to an action macro. The shared procedure can then change that button and find the target figure on the currently displayed slide. This runs on a click during Slide Show mode, not automatically when a slide appears.
On the first slide, open Home > Select > Selection Pane and rename the figure to Plot1. Use the same name for the target figure on every slide where you will use the shared toggle.
Open Developer > Visual Basic, choose Insert > Module, and paste the following code into the standard module: Public Sub Toggle(ByVal oshp As Shape) Dim currentSlide As Slide Dim target As Shape If SlideShowWindows.Count = 0 Then Exit Sub Set currentSlide = SlideShowWindows(1).View.Slide On Error Resume Next Set target = currentSlide.Shapes("Plot1") On Error GoTo 0 If target Is Nothing Then MsgBox "No shape named Plot1 was found on this slide." Exit Sub End If Select Case oshp.Fill.ForeColor.RGB Case vbRed oshp.Fill.ForeColor.RGB = vbGreen target.Visible = msoTrue Case Else oshp.Fill.ForeColor.RGB = vbRed target.Visible = msoFalse End Select End Sub Public Sub ConfigureSelectedToggle() Dim button As Shape If ActiveWindow.Selection.Type <> ppSelectionShapes Then MsgBox "Select the toggle shape first." Exit Sub End If Set button = ActiveWindow.Selection.ShapeRange(1) With button.ActionSettings(ppMouseClick) .Action = ppActionRunMacro .Run = "Toggle" End With End Sub
Return to the slide and choose Insert > Shapes to draw a regular shape or action button. Select it, then open Developer > Macros, select ConfigureSelectedToggle, and click Run. This assigns the shape's mouse-click action to the Toggle procedure.
With the button selected, choose Shape Format > Shape Fill > Green if Plot1 starts visible, or Red if Plot1 starts hidden. Copy the configured button and paste it onto the other slides. Confirm in each slide's Selection Pane that its target figure is named Plot1.
Choose Slide Show > From Current Slide and click the button. A green button turns red and hides Plot1; a red button turns green and shows Plot1. Save the presentation as .pptm to retain the VBA project.

Use WPS Presentation for Everyday Slide Editing
WPS Office is a free, lightweight alternative for everyday presentation work, with a familiar interface and support for PowerPoint .ppt and .pptx files to simplify migration. It is not a fix for PowerPoint VBA event wiring; keep this macro-driven workflow in a PowerPoint environment that supports the required VBA features.

Frequently Asked Questions
Why doesn't TB1_Click run after I import it into a standard module?
TB1_Click is an event procedure tied to a particular control when it is placed in the appropriate slide object module. Moving that name into a standard module does not preserve the event connection. Use a regular shape with a Run Macro action to call a public shared procedure instead.
Do I need to import the VBA module separately for each slide?
No. VBA belongs to the presentation's VBA project, not to individual copies of each slide. Add the shared procedure once through Developer > Visual Basic > Insert > Module, then connect buttons on multiple slides to it.
What if Toggle does not appear in the Run Macro list?
A procedure that takes a Shape argument may not be offered in the macro picker. Select the button and run the no-argument ConfigureSelectedToggle procedure from Developer > Macros. It assigns ActionSettings(ppMouseClick).Run to Toggle directly.
Can the toggle target the current slide instead of always using Slides(1)?
Yes. During Slide Show mode, SlideShowWindows(1).View.Slide returns the displayed slide. The example uses that slide's Shapes("Plot1") collection, so each copied button acts on its own slide's figure.
Will this code run automatically when I advance to a slide?
No. A shape's mouse-click action runs only when you click it during Slide Show mode. For slide-entry automation, use PowerPoint application events such as SlideShowNextSlide in an initialized WithEvents class; importing a standard module alone does not establish that event subscription.




