How do I only delete certain slides in a presentation?

One thing you’re likely to discover when developing a robust approach for automatic updates is that you might have to come up with a way of scrapping the previous update and replacing it with the latest one.

At least that’s what I came to painfully realize a month after I fully coded my PowerPoint slides consolidation

The way I chose to go about it was by deleting slides on the basis of their SlideIDs, which unlike the Slide indexes that indicate the position of a slide in a presentation, will not change when new slides have been added or removed.

This would work great as I needed to remove everything but the section header slides, whose Slide IDs I hard-coded in the code below:

Sub Delete_Slides_on_SlideIDs()
Dim Ppres As Presentation
Set Ppres = ActivePresentation
Dim Sh As Shape
Dim i As Integer
Dim PPS As Slide
'******************************************************
'used in Delivery Reviews
For Each PPS In Ppres.Slides
If PPS.SlideID <> 1651 And PPS.SlideID <> 1543 And PPS.SlideID <> 1711 _
And PPS.SlideID <> 1683 And PPS.SlideID <> 1684 _
And PPS.SlideID <> 1439 And PPS.SlideID <> 1433 And PPS.SlideID <> 1440 _
And PPS.SlideID <> 1434 And PPS.SlideID <> 1442 And PPS.SlideID <> 1441 _
And PPS.SlideID <> 1502 And PPS.SlideID <> 1435 And PPS.SlideID <> 1436 _
And PPS.SlideID <> 1437 And PPS.SlideID <> 1438 Then PPS.Delete
Next PPS
End Sub

As time progressed, I discovered that I also needed to have a more custom approach in place to cater for those occasions when I had to update slides due to erroneously generated data.

One approach would be to ask for user input and delete the slides that contain a specified word in their header.

Sub Delete_Slides_TextinTitle()
Dim Ppres As Presentation
Set Ppres = ActivePresentation
Dim PPS As Slide
Dim StrNo As Long
Dim Titel As String
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For Each PPS In Ppres.Slides
'slide has a certain text in Slide Title
Titel = PPS.Shapes.Title.TextFrame.TextRange
StrNo = InStr(Titel, "Project")
If StrNo <> 0 Then PPS.Delete
Next PPS
End Sub

Yet another approach could be that only slides that have a certain layout or number of shapes get deleted

Sub Delete_Slides_other_criteria()
Dim p As Presentation
Set p = ActivePresentation
Dim Sh As Shape
Dim PPS As Slide
Dim i As Integer
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'slides with a certain layout
For Each PPS In Ppres.Slides
If PPS.Layout = ppLayoutSectionHeader Then PPS.Delete
If PPS.Layout = ppLayoutBlank Then PPS.Delete
If PPS.Layout = ppLayoutChart Then PPS.Delete
If PPS.Layout = ppLayoutPictureWithCaption Then PPS.Delete
Next PPS
'slides that have more than a certain number of shapes on them
On Error Resume Next
For i = 2 To Ppres.Slides.Count
If Ppres.Slides(i).Shapes.Count > 1 Then
Ppres.Slides(i).Delete
Next i
End Sub

But how do I know how many shapes a slide has?

Easy! You can use the code below to have the shape count shown in a message box upon entering a slide’s presentation position.

Sub Count_shapes_on_slide()
Dim p As Presentation
Set p = ActivePresentation
Dim No As Integer
Dim i As Integer
Dim PPS As Slide
Dim Sh As Shape
'***************************
i = InputBox("Enter Slide number")
'shows number of shapes on slide
No = p.Slides(i).Shapes.Count
MsgBox (No) & " shapes found"
For Each Sh In p.Slides(i).Shapes
If Sh.Type <> msoPicture Then MsgBox (Sh.Type)
Next Sh
End Sub

Happy VBA coding!