I've got a Do Until
loop in my code which in some circumstances may error. However due to the error, I only want the code to exit the Do Until
loop and continue with the rest of the subroutine.
I've tried using On Error Exit Do
but this says
Expected GoTo or Resume
Then I tried using On Error GoTo 5865
, as 5865 is the line after the loop that it needs to continue executing from, however, this says
Label not defined
How am I able to make this happen?
You declare an error trapper at the top of your SUB or FUNCTION, i.e.:
On Error Goto ErrTrap
In this case, "ErrTrap" is a label, and it would typically be at the bottom of your routine:
ErrTrap:
If Err.Number = ... Then
...
Resume AfterError ' another label
End If
MsgBox("Error blah blah")
Above "ErrTrap:" you would place an
Exit Sub
or Exit Function
accordingly
and you would place "AfterError:" wherever you want the code to resume after a handled error.
This is a simplification of a much more complex subject - there are a lot of options to error handling, including line number trapping, Resume Next, etc., but this should be enough to get you what you want for now. Research the subject for more information.