Billy Hill
Board Regular
- Joined
- Dec 21, 2010
- Messages
- 73
I'm using multiple "ON ERROR" calls in a macro but I keep getting errors after the second iteration.
I'm using the Cells.Find function when I get the error. Here's some of the code and the error I get:
On Error GoTo NextOperation
Do While Cells.Find(What:="cont'd", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireRow.Delete
ActiveCell.Offset(-1, 0).EntireRow.Delete
Loop
NextOperation:
On Error GoTo 0
Range("A1").Select
Selection.Value = "PN"
Range("B1").Value = "Temp"
Range("C1").Value = "TotQTY"
On Error GoTo NextOperation1
Do While Cells.Find(What:="Stock code", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireRow.Delete
ActiveCell.Offset(-1, 0).EntireRow.Delete
ActiveCell.Offset(-2, 0).EntireRow.Delete
Loop
NextOperation1:
The second loop will go all the way through until the last line is found then error with "Run-time error 91: Object variable or With block variable not set" after it can't find any more instances. (I'm using the error generated to know that I'm at the end of the file and need to move on to the next find.)
I've tried to reset the error handling by using On Error GoTo 0 but still getting the error.
That part of the macro will run if I only run that part (I run to that point, stop the macro, then restart it and skip back to that line and start from there) so I'm sure the code works with only one On Error call.
Is there something else I need to do to reset the On Error handle besides On Error GoTo 0?
TYIA
I'm using the Cells.Find function when I get the error. Here's some of the code and the error I get:
On Error GoTo NextOperation
Do While Cells.Find(What:="cont'd", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireRow.Delete
ActiveCell.Offset(-1, 0).EntireRow.Delete
Loop
NextOperation:
On Error GoTo 0
Range("A1").Select
Selection.Value = "PN"
Range("B1").Value = "Temp"
Range("C1").Value = "TotQTY"
On Error GoTo NextOperation1
Do While Cells.Find(What:="Stock code", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireRow.Delete
ActiveCell.Offset(-1, 0).EntireRow.Delete
ActiveCell.Offset(-2, 0).EntireRow.Delete
Loop
NextOperation1:
The second loop will go all the way through until the last line is found then error with "Run-time error 91: Object variable or With block variable not set" after it can't find any more instances. (I'm using the error generated to know that I'm at the end of the file and need to move on to the next find.)
I've tried to reset the error handling by using On Error GoTo 0 but still getting the error.
That part of the macro will run if I only run that part (I run to that point, stop the macro, then restart it and skip back to that line and start from there) so I'm sure the code works with only one On Error call.
Is there something else I need to do to reset the On Error handle besides On Error GoTo 0?
TYIA
Last edited: