Error Handling

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I must have misunderstood how it works
VBA Code:
Sub isntworking()
    Dim Comm As Range
    Dim cl As Range
    Dim x As Integer
    With Worksheets("Sheet1")
           For x = 2 To 10
                Set iRange = .Range("A" & x & ":CV" & x)
                On Error GoTo NoExcelComments ' handle the error that occurs if there is no cell with comments
                Set Comm = iRange.SpecialCells(xlCellTypeComments)
                For Each cl In Comm ' iterate over all cells in rng - each of them contains a comment
                    Debug.Print cl
                Next
NoExcelComments:
        Next
    End With
End Sub
Stepping show the first row with no comments goes to the Error and Next.
But the next instance crashes with error 1004 No Cells Were found.
But hasn't the error handler been reset?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
The error handler should be set each time through the loop. It sounds like you are saying that you are raising an error in the next line after On Error.

I'm not sure why you are seeing that behavior, but I am not a fan of using error handling to manage the logic path. If you know that there can be no comments, it's not really an error and should be tested for at the point it occurs, rather than using it as a GoTo. IMHO as a software developer I would treat a raised error as an unexpected and unrecoverable condition.

Unfortunately the Microsoft engineers did not allow SpecialCells to return Nothing. If there are no cells in that category it raises an error. I would suggest capturing the error and handling it immediately when it occurs, rather than using a GoTo. Try the following:

Rich (BB code):
Sub isntworking()
    Dim Comm As Range
    Dim cl As Range
    Dim x As Integer
    With Worksheets("Sheet1")
           For x = 2 To 10
                Set iRange = .Range("A" & x & ":CV" & x)
                On Error Resume Next
                Set Comm = iRange.SpecialCells(xlCellTypeComments)
                If Err.Number = 0 Then
                   On Error GoTo 0
                   For Each cl In Comm ' iterate over all cells in rng - each of them contains a comment
                       Debug.Print cl
                   Next
               Else
                   On Error GoTo 0
               End If
        Next
    End With
End Sub
 
Upvote 0
Not real sure what the goal is there but can say that generally speaking you don't attempt to use a property (e.g. .Value) and then trap an error because it isn't available or valid. Instead, you test its value first; e.g. >0, Is Nothing, Is Not Nothing, <>"", etc. and code executes according to the test result. An exception would be to try to use an object or property that may not exist - e.g. try to use Outlook and if it errors, create an instance of it (because you can't rely on it being loaded).

For your case it may be better to test if a comment exists, otherwise you are attempting to set a range object based on it having a comment, and if it doesn't, that will fail. This may help you with your question about error handling
 
Upvote 0
you test its value first; e.g. >0, Is Nothing, Is Not Nothing, <>"",
This is the textbook approach but does not work with SpecialCells. If there are no cells in the specified category, SpecialCells does not return Nothing. It raises an error. This is terrible language design and requires a clunky workaround. But here we are.
 
Upvote 0
But hasn't the error handler been reset?
Not quite. You have to clear the current exception state - usually by using a Resume statement - otherwise any further errors will be unhandled.
 
Upvote 0
SpecialCells does not return Nothing
"Nothing" is for objects. Those were only meant to be examples as they apply. I don't know much about special cells but if it's a property then perhaps you test for its existence also, just like the .Value prpoerty - but test if it exists, not the value.
 
Upvote 0
SpecialCells is a method but it either returns a range or raises a run-time error, rather than gracefully returning Nothing. It could, as Jeff said, have been better implemented. (it's not alone in that respect!)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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