Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA On Error handling - jump more than one line of code on error

Status
Not open for further replies.

wakayama

Programmer
Feb 6, 2009
3
GB
I want my programme to jump to Err 1 when it detects an error in this loop, but this doesn't seem to be working (i.e. I still get run-time error message '13'). Can anyone spot any problem with my syntax?


Private Sub groups_Click()

On Error Resume Next
For i = 0 To 100
market.attribute.Selected(i) = False
Next i
For i = 1 To 100
market.attribute.RemoveItem 0
Next i
On Error GoTo 0

'Below is the one I have a problem with - if it detects an error in the loop below I want it to jump to Err1 (i.e. go to next n).

On Error GoTo Err1
For n = 3 To 65000

If seg_data.Cells(n, 10) = "" Then
Exit For
End If

If seg_data.Cells(n, 4) = market.Company.Value And seg_data.Cells(n, 5) = market.main.Value And seg_data.Cells(n, 6) = market.groups.Value Then
market.attribute.AddItem seg_data.Cells(n, 7)
End If

Err1:

Next n

market.Activate

End Sub
 
Hi,

Exactly which statement is the error occurring on?

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
VBA Error trapping bites anew!!

The first error you get activates the error trap, thus disabling it for trapping further errors. As you never Resume, the next error is not trapped.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
...
On Error GoTo Err[!]2[/!]
For n = 3 To 65000
...
Err1:
Next n
market.Activate
[!] Exit Sub
Err2:
Resume Err1[/!]
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
As an aside, surely it would make more sense to code so that the error doesn't happen (as you know where this error will be thrown) and leave the error handling routine to handle unforseen errors?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top