22

I have looked online as much as I could (except for the Microsoft support website, which is blocked at work for some reason). I am trying to simply skip an error. My code written here is simplified but should work the same way.

What my code is supposed to do: One of my subs creates shapes in a loop and names them (btn_1, btn_2, etc). But before creating them, it calls a sub that tries to delete them so as not to create duplicates. This sub loops through (btn_1, btn_2, etc) and deletes the shapes using:

for i = 1 to (a certain number)
    Set shp = f_overview.Shapes("btn_" & i)
    shp.delete
next

Of course, it happens that the shape cannot be deleted because it simply does not exist. I have found that most of the time, the reccomended fix is to add (on error resume next) before setting the shape, as I get an error saying it does not exist. I have tried it inside the loop, before the loop, etc, like so:

for i = 1 to (a certain number)
    On Error Resume Next
    Set shp = f_overview.Shapes("btn_" & i)
    shp.delete
next

As far as I understand it is supposed to loop right through if the shape doesn't exist, but I still get the same error whether or not I add the On error resume next! What am I doing wrong?

EDIT: There is no error when the shapes do exist.

5 Answers 5

30

I have found that most of the time, the reccomended fix is to add (on error resume next) before setting the shape, as I get an error saying it does not exist.

NO!

The recommended way to handle runtime errors is not to shove them under the carpet and continue execution as if nothing happened - which is exactly what On Error Resume Next does.

The simplest way to avoid runtime errors is to check for error conditions, and avoid executing code that results in 100% failure rate, like trying to run a method on an object reference that's Nothing:

For i = 1 To (a certain number)
    Set shp = f_overview.Shapes("btn_" & i)
    If Not shp Is Nothing Then shp.Delete
Next

In cases where you can't check for error conditions and must handle errors, the recommended way is to handle them:

Private Sub DoSomething()
    On Error GoTo CleanFail

    '...code...

CleanExit:
    'cleanup code here
    Exit Sub

CleanFail:
    If Err.Number = 9 Then 'subscript out of range
        Err.Clear
        Resume Next
    Else
        MsgBox Err.Description
        Resume CleanExit
    End If
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

++ on Proper Error Handling :)
@SiddharthRout yeah, except I didn't test it and now the more I think of it, the more I believe the assignment would throw an index out of range error, in which case FreeMan's answer has a better solution.
That;s ok :) Edit your above code. My comment was the proper error handling that you showed t the bottom of the post. i did mention about that in my post as well :)
@SiddharthRout edited, ...without stealing from anyone's answer :)
No need to explicitly "Err.Clear" before the Resume Next. As for the MS Docs "The Err object's properties are reset to zero or zero-length strings ("") after an Exit Sub, Exit Function, Exit Property, or Resume Next statement within an error-handling routine. Using any form of the Resume statement outside of an error-handling routine will not reset the Err object's properties. The Clear method can be used to explicitly reset Err."
|
16

There is nothing WRONG in using OERN (On Error Resume Next) provided you understand what you are doing and how it is going to affect your code.

In your case it is perfectly normal to use OERN

Dim shp As Shape

For i = 1 To (a certain number)
    On Error Resume Next
    Set shp = f_overview.Shapes("btn_" & i)
    shp.Delete
    On Error GoTo 0
Next

At the same time ensure that you don't do something like

On Error Resume Next
<Your Entire Procedure>
On Error GoTo 0

This will suppress ALL errors. Use proper error handling as shown by Matt

Edit:

Here is another beautiful example on how to use OERN This function checks if a particular worksheet exists or not.

Function DoesWSExist(wsName As String) As Boolean
    Dim ws As Worksheet

    On Error Resume Next
    Set ws = ThisWorkbook.Sheets(wsName)
    On Error GoTo 0

    If Not ws Is Nothing Then DoesWSExist = True
End Function

If you wish you can also loop through all the sheets to check is the sheet exists or not!

2 Comments

Thanks for the explanation! The reason it was failing, though, was because I had the wrong settings (see chosen answer).
++ but unfortunately most don't understand the implications of using OERN. I like that acronym btw. I'm keeping it.
14

Instead of trying to blindly delete shapes and skipping errors, why not run through the list of known shapes and delete them. Then you don't have to worry about an On Error Resume Next which often ends up being abused.

Sub Test(TheSheet As Worksheet)

Dim Shp as Shape

For Each Shp in TheSheet.Shapes
  If left(Shp.Name, 4) = "btn_" Then
    Shp.Delete
  End if
Next

End Sub

If you want to delete all shapes, remove the If statement. If you want to delete a number of differently named shapes, modify the If statement appropriately.

4 Comments

AH excellent! This is the way I will choose to do it. Thank you for the fix. +1 for answer even though you don't explain how to use error handling :-)
You are correct on the lack of error handling detail - this loop doesn't require it, especially not in the way you were thinking. Mat's Mug does a fine job of covering error handling in general.
It's OK, @FreeMan, 3 months after that, there was a random drive-by upvote! :)
9 years later and another random down vote. Anyone care to share what's wrong with this so I can learn something?
10

It sounds like you have the wrong error trapping option set. Within the VBA Editor, Select Tools -> Options. In the window that opens, select the General tab, and pick the Break on Unhandled Errors radio button. This should allow Excel to properly process the On Error Resume Next command.

I suspect that you have Break on All Errors selected.

2 Comments

Thanks! I kept trying the suggestions of the other answerers and all failed. This was why although they also provided valuable information!
@DavidGrand'Maison I agree that the error handling being done in the other answers is cleaner and better. But your original code should have worked.
3

Try:

On Error Resume Next

for i = 1 to (a certain number)
    Set shp = f_overview.Shapes("btn_" & i)
    if err<>0 then err.clear else shp.delete
next

on Error Goto 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.