PowerPoint VBA: How to Loop Through Slides

VBA & macros · Updated November 18, 2026

To loop through every slide in the open PowerPoint presentation, use a VBA For Each loop over ActivePresentation.Slides. For example, the macro below prints each slide’s position and name in the Immediate window:

Sub ListSlides()
    Dim pres As Presentation
    Dim sld As Slide

    Set pres = ActivePresentation

    For Each sld In pres.Slides
        Debug.Print sld.SlideIndex, sld.Name
    Next sld
End Sub

This is the usual starting point: get the presentation, visit each slide in sequence, and put the action you need inside the loop. Use an indexed loop instead when you need a slide’s position, and add a second loop when the task also needs to inspect shapes on each slide.

What a PowerPoint slide loop does

In PowerPoint’s object model, a presentation contains a Slides collection, and each item in that collection is a Slide object. A loop repeats instructions once for each item. The loop does not itself change the slides; the statements inside it determine whether you inspect names, read text, apply formatting, or make another change.

ActivePresentation means the presentation that is currently active in PowerPoint. The examples here assume that a presentation is open and that you are running the code in that context. If a macro is stored in a separate presentation or add-in, make sure it targets the intended file rather than assuming the active one is always the right target.

A loop over pres.Slides follows the presentation’s slide order. It visits slides in the collection, including slides hidden from a slide show. That distinction matters: the set of slides in a presentation is not necessarily the same as the subset shown in a custom slide show.

Use For Each for a straightforward pass

A For Each loop is usually the clearest choice when the operation is the same for every slide. The variable sld represents the current slide during that pass. PowerPoint advances it to the next slide when VBA reaches Next sld, then exits after the final item.

For instance, the opening macro lists slide indexes and names. To do a different task, replace the Debug.Print line with the work you want to perform. Debug.Print sends information to the Visual Basic Editor’s Immediate window; it does not add text to a slide or display a dialog box.

This style is readable and avoids manually managing a counter. It is a good fit for reporting, checking slide properties, or applying a consistent operation. If you need to refer to a slide by its position, compare positions, or count through a range, use the indexed form instead.

Use an indexed loop when position matters

The Slides collection is indexed starting at 1. An indexed loop lets you retrieve each slide using pres.Slides(i) and use i in your logic. Declare the counter as Long, which is the standard integer type for collection indexes in VBA.

Sub ListSlidesByIndex()
    Dim pres As Presentation
    Dim sld As Slide
    Dim i As Long

    Set pres = ActivePresentation

    For i = 1 To pres.Slides.Count
        Set sld = pres.Slides(i)
        Debug.Print i, sld.Name
    Next i
End Sub

Here, pres.Slides.Count gives the number of slides, and i identifies the current position. The SlideIndex property also reports a slide’s current position in the presentation. Do not treat that position as a permanent identifier: inserting, deleting, or reordering slides can change indexes. A slide’s Name is useful for human-readable identification, while its SlideID is a distinct identifier within the presentation.

Loop through shapes on each slide

Many slide tasks need a nested loop: the outer loop visits slides, and the inner loop visits the shapes on the current slide. A shape can be a text box, picture, chart, placeholder, or other object, so check that it has a text frame and actual text before trying to format its text.

Sub SetTextShapeFontSize()
    Dim pres As Presentation
    Dim sld As Slide
    Dim shp As Shape

    Set pres = ActivePresentation

    For Each sld In pres.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame = msoTrue Then
                If shp.TextFrame.HasText = msoTrue Then
                    shp.TextFrame.TextRange.Font.Size = 18
                End If
            End If
        Next shp
    Next sld
End Sub

This example sets text in slide shapes to 18 points. It is intentionally broad: it affects all text-bearing shapes, which may include titles, footers, captions, and notes placed on the slide itself. If only titles should change, add a condition that identifies title placeholders rather than applying the formatting to every text shape. Also note that the loop over sld.Shapes does not automatically walk into individual members of a grouped shape; groups need an additional traversal of their group items.

Common tasks and useful variations

The work inside a slide loop can be as small as printing a property or as substantial as auditing and editing slide content. A few practical patterns are:

  • Build an inventory: print each slide’s index and name, or inspect its shape count with sld.Shapes.Count.
  • Check text: inspect text-bearing shapes, while accounting for the fact that some shapes do not contain text.
  • Apply formatting: change a property only after checking that the object supports it and that the change is appropriate.
  • Skip a slide: put an If condition inside the loop so the action runs only when a slide meets your criteria.

For example, to process only slides that are not hidden, test the slide’s show-transition setting before doing the task: If sld.SlideShowTransition.Hidden = msoFalse Then. Place the desired action inside that condition. This filters hidden slides but does not change the collection or the order of the remaining slides.

Choosing between For Each and an indexed loop

Choose For Each when you simply need to act on every slide. The loop reads naturally and gives you a direct reference to the current Slide. Choose an indexed loop when the position is part of the logic—for example, when you need to compare a slide with the previous one or process a particular range.

There is an important caution when changing the collection during iteration. If a macro deletes slides while moving forward through an indexed collection, the later slides shift positions; a loop can then skip an item or refer to a different slide than expected. A reverse indexed loop is often used for deletion because removing a later item does not shift the indexes that are still to be visited. For ordinary inspection or formatting, do not complicate the loop: use the pattern that matches the task.

If your task depends on what an audience sees, clarify whether it should include hidden slides or follow a custom slide show. A regular pass through pres.Slides visits the presentation’s slide collection, not necessarily a custom show’s selected sequence.

Common errors and how to avoid them

A frequent mistake is using an unqualified Slides reference in a macro that could run with more than one presentation open. Assign ActivePresentation to a variable and use pres.Slides throughout, or explicitly set the presentation you intend to work on. This makes the target easier to understand and reduces accidental edits to the wrong file.

Another error is assuming every shape contains text. A picture or line does not have usable text to format, so code that jumps straight to shp.TextFrame.TextRange can fail. The two checks in the nested example handle that distinction. If you work with charts, tables, groups, or other specialized objects, their contents may require object-specific code beyond a general shape loop.

Be aware of how indexes change. Slides(1) means the first slide in the current order, not a permanent identity. If code stores an index and then rearranges slides, that stored number may point somewhere else afterward. Avoid changing the collection while traversing it unless the loop is designed for that change.

Finally, VBA errors can be caused by a missing reference or by code being run in the wrong Office application. PowerPoint-specific types such as Presentation, Slide, and Shape belong in PowerPoint VBA. Run the examples from PowerPoint’s Visual Basic Editor, not Excel’s, unless you are deliberately automating PowerPoint from Excel and have set up the required object references or late binding.

Run the macro and protect the presentation

In PowerPoint for Microsoft 365 on Windows, open the Visual Basic Editor from Developer > Visual Basic, or press Alt+F11. If the Developer tab is not visible, enable it through File > Options > Customize Ribbon. In the editor, insert a standard module, paste the macro, and run it with the cursor inside the procedure or from PowerPoint’s macro list using Alt+F8.

On Mac, open the Visual Basic Editor through Tools > Macro > Visual Basic Editor; menu labels and keyboard behavior can vary by PowerPoint version and keyboard settings. Save a macro-enabled presentation as a .pptm file if the VBA needs to remain in that presentation. Macro security settings may block macros, so only enable or run code from sources you trust.

Before running a macro that changes slides, save a separate copy of the presentation. VBA actions generally cannot be reversed with PowerPoint’s Undo command or Ctrl+Z on Windows (or Command+Z on Mac). The font-size example makes direct edits, so a backup is especially important if the presentation contains carefully formatted text.

Frequently Asked Questions

How do I loop through all slides in PowerPoint VBA?
Use For Each sld In ActivePresentation.Slides, with sld declared as a Slide. Put the action to repeat inside the loop and close it with Next sld.

Does the Slides collection include hidden slides?
Yes. A loop through Presentation.Slides includes slides hidden from a slide show. Add a test of sld.SlideShowTransition.Hidden if you want to skip hidden slides.

How do I loop through shapes on every slide?
Put a second loop inside the slide loop: For Each shp In sld.Shapes. Check the shape’s properties before accessing text or other specialized content, because not every shape contains text.

Should I use For Each or For i = 1 To Slides.Count?
Use For Each for a straightforward pass through every slide. Use an indexed loop when you need the slide’s position, process a range, or need to traverse backward while removing items.

Why does my macro skip slides after deleting one?
Deleting a slide shifts the indexes of later slides. A forward loop may advance past the slide that shifted into the deleted slide’s position. When deletion is necessary, use a carefully designed reverse indexed loop and test it on a copy first.

Bottom line

For most PowerPoint VBA tasks, loop through ActivePresentation.Slides with For Each, then add an indexed loop or a nested shape loop only when the job calls for it. Check object properties before editing, decide how hidden slides should be treated, and preserve a copy before running code that changes the presentation.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.