VBA Error Handling Loop Not Working Second Time Around

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
Hopefully this is something that is an easy fix that I am overlooking. I am wanting to loop through each slide and essentially skip slides that do not contain a shape called Graph2 or Graph3. My error handler works fine in skipping the Title Slide however the second time around I need to handle an error, I actually get an error message popping up. I believe I am not resetting my error handler properly. Any thoughts?

Code:
Sub ErrorHandlingProblem()

Dim sld As Slide
Dim shpSelected As Shape
Dim shpTrigger As Shape


For Each sld In ActivePresentation.Slides
  On Error GoTo SkipSlide
    Set shpSelected = sld.Shapes("Graph3")
    Set shpTrigger = sld.Shapes("Graph2")
  On Error GoTo 0
  
  '[Do Something...]
  
SkipSlide:
Err.Number = 0


Next sld


End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You have to reset it to 0 after the Handler line

Code:
SkipSlide:
Err.Number = 0
On Error GoTo 0


Next sld
 
Upvote 0
Unfortunately that did not work. But it did somehow give me another idea and this seems to work in PowerPoint:

Code:
Sub ErrorHandlingProblem()

Dim sld As Slide
Dim shpSelected As Shape
Dim shpTrigger As Shape


For Each sld In ActivePresentation.Slides
  On Error Resume Next
    Set shpSelected = sld.Shapes("Graph3")
    Set shpTrigger = sld.Shapes("Graph2")
  
  If Err.Number = -2147188160 Then GoTo SkipSlide
  '[Do Something...]
  
SkipSlide:
Err.Number = 0


Next sld


End Sub

I know PowerPoint VBA can be a little sketchy so I wonder if it was just acting up for no reason in the way I was trying to handle the error before. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top