VBA & Macro Problems

Access VBA Variables Across PowerPoint Slides

Huma Ashraf ChHuma Ashraf Ch Nov 14, 2026 869 views

Question details

The user needs to access a VBA variable from procedures associated with different PowerPoint slides.

Access VBA Variables Across PowerPoint Slides
Product
Microsoft PowerPoint
Device & OS
not provided
Scenario
A public variable named UserName is declared in a slide code module and needed by another procedure.
Observed behavior
The procedure cannot access the variable using Slide2.UserName.
Before you start

Open the presentation in PowerPoint and make sure it contains the slide and text-capable shape you want to update. Save it as a PowerPoint Macro-Enabled Presentation (.pptm) if it is not already macro-enabled.

Solution 1Recommended

Declare the shared variable in a standard VBA module

Use a public variable in a standard module so procedures can reference it directly by name.

Slide2 is not normally a VBA object reference, and UserName is not a built-in slide property. Dot syntax accesses an object's properties or methods; it does not make a variable in a slide code module a slide property.

1
Open the VBA editor

In PowerPoint, open Developer > Visual Basic. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.

2
Insert a standard module

In the VBA editor, select the presentation project, then choose Insert > Module.

3
Declare and set the variable

At the top of the new module, outside any procedure, enter `Public UserName As String`. In a procedure that runs before the value is needed, set it with an assignment such as `UserName = "Alex"`.

4
Reference the variable directly

In the procedure that needs the value, use `UserName` rather than `Slide2.UserName`. For example, to write it to the second shape on the second slide of the active presentation, use `ActivePresentation.Slides(2).Shapes(2).TextFrame.TextRange.Text = UserName`.

5
Run and check the macro

Return to PowerPoint, open Developer > Macros, and run the procedure that sets UserName before running the procedure that displays it. Check the target shape on slide 2.

Check the target shape: The example assumes the active presentation has at least two slides and that shape 2 on slide 2 can hold text. Adjust the slide and shape references for your presentation.
Free Microsoft Office alternative

Edit PowerPoint presentations with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing presentations. Its PowerPoint format support can help you move existing files into your workflow, but it does not fix PowerPoint VBA issues; check macro support before relying on VBA code.

Create and edit slide content in WPS PresentationOpen and edit Microsoft PowerPoint PPTX filesUse a familiar interface for a smoother transition from OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Where should I declare a variable shared by PowerPoint macros?

In the VBA editor, choose Insert > Module and declare it at the top of the standard module, outside any procedure, using a statement such as `Public UserName As String`.

Why doesn't Slide2.UserName work?

Slide2 is not normally a VBA object reference, and UserName is not a built-in slide property. Declare the variable in a standard module and reference it directly as `UserName`.

How do I show the variable's value on a slide?

Assign it to a text-capable shape's text range, for example: `ActivePresentation.Slides(2).Shapes(2).TextFrame.TextRange.Text = UserName`. Change the slide and shape references to match your presentation.

Why is my public variable empty when the display macro runs?

A declared string has an empty value until it is assigned. Run the procedure that sets UserName before the procedure that reads or displays it.