PowerPoint Macro: Deleting Slides After the Active Slide

This PowerPoint VBA macro deletes slides after the active slide.

Macro Example

Sub DeleteSlides()

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

  With ActivePresentation.Slides
 
    For i = .Count To sld_id Step -1
      .Item(i).Delete
    Next i
 
  End With

End Sub

Description

Variable Declaration

Sub...

  Dim i As Long
  Dim sld_num As Long

The 3rd line declares a long integer type variable named i that is used in the For…Next looping.
And the 4th line declares a long integer type variable named sld_num that is used for charging the index number of an active slide.

Assigning a Value

Sub...

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

The 6th line assigns a value to the variable: sld_id .

Starts With

Sub...

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

  With ActivePresentation.Slides

The 8th line starts the With…End With statement.
This statement tells PowerPoint that following any action apply to the ActivePresentatin.Slides (Presentation.Slides object).

[Application.]ActivePresentation

The Application.ActivePresentation property returns the active Presentation object.

obj-PowerPoint-Application-ActivePresentation

The ActivePresentation property is a member of the PowerPoint.Global class, so we can omit the Application property.

obj-PowerPoint-ActivePresentation

ActivePresentation.Slides [Presentation.Slides]

The ActivePresentation.Slides (Presentation.Slides property) returns the Slides collection object.

obj-PowerPoint-Presentation-Slides

The Slides object is a collection of all the Slide object in the presentation.

obj-PowerPoint-Slides

Starts Looping

Sub...

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

  With ActivePresentation.Slides
 
    For i = .Count To sld_id Step -1

The 10th line starts to loop through the elements (Slide) of a collection (Slides).

The number variable i starts at the max count of the slide (Slides.Count) and is reduced by 1 on each iteration of the loop, ending after the value of variable reaches sld_id.

The Slides.Count property returns the number of slides in the presentation.

obj-PowerPoint-Slides-Count

Deleting the Slide

Sub...

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

  With ActivePresentation.Slides
 
    For i = .Count To sld_id Step -1
      .Item(i).Delete

The 11th line deletes the slide.

Slides.Item

The Slides.Item method returns a single Slide object from a Slides collection.

obj-PowerPoint-Slides-Item

Slide.Delete

The Slide.Delete method deletes the specified Slide object.

obj-PowerPoint-Slide-Delete

Loops back

The 12th line loops back to evaluate the next slide.
And the 14th line ends the With…End With statement for the ActivePresentation.Slides.

Sub DeleteSlides()

  Dim i As Long
  Dim sld_id As Long

  sld_id = ActiveWindow.Selection.SlideRange.SlideIndex

  With ActivePresentation.Slides
 
    For i = .Count To sld_id Step -1
      .Item(i).Delete
    Next i
 
  End With

End Sub

Properties

http://www.relief.jp/itnote/archives/powerpoint-vba-delete-slides.php

Apply to

  • PowerPoint 2013
  • PowerPoint 2010
  • PowerPoint 2007