Re: On Error GoTo stops working

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



You are not really using the Error object correctly... the On Error GoTo's code (where the statement sends execution when an error occurs) should not be inside a loop. To properly run an On Error GoTo trap, you need a Resume statement (there are 3 variations for this statement) inside the code the error sends execution to in order for it to take you out of the error code and back into your main code... you cannot do that from inside a loop (well, you might be able to doctor up a kludge to do it, but it would be horrible programming practice to do so). You might be able to use and On Error Resume Next and do testing for Err.Number to see if an error occurred and direct your code that way, but it is hard to give you advice because of your "(more code)" sections. Try looking up the "On Error Statement" and the "Resume Statement" in the help files to get an idea of how that combination is meant to work.

--
Rick (MVP - Excel)


"Noemi" <Noemi@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:02644E21-3FCA-4410-94A0-D4BEB9928031@xxxxxxxxxxxxxxxx
Hi

I have on On Error Goto inside a loop and the first time it has an error it
goes to the section which performs are task but the second time it has an
error it crashs and I get the error 9 message.

Here is my code

Dim stName As String
Dim stThe As String
Dim stLetter As String
Dim dbLong As Double

Sheets("Temp").Select
Range("A2").Select

Do

stName = ActiveCell.Value
stLetter = Left(stName, 1)
If stLetter = "Y" Or stLetter = "Z" Then
stLetter = "X-Z"
End If
On Error GoTo BoldLine
Sheets(stLetter).Select
(more code)
BoldLine:
Sheets("Temp").Select
ActiveCell.Font.Bold = True
(more code)
ActiveCell.Offset(1, 0).Select
Loop Until ActiveCell.Value = ""

Thanks Noemi

.


Quantcast