ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error Handling problem (https://www.excelbanter.com/excel-programming/329984-error-handling-problem.html)

Brassman[_5_]

Error Handling problem
 
Not sure why excel is doing this... I have this code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/23/2005 by andya
Dim c As OLEObject
For x = 1 To Worksheets.Count
On Error GoTo ErrHand
For Each c In Worksheets(x)
'do stuff
Next c
ErrHand:
Next x

End Sub

This is what happens. When I step through the code the error handling works
fine when x=1. But on the next trip through (x=2), It gives me Run-time
error '438' on the "For Each c in Worksheets(x)" line and doesn't use the
error handling. Why?? Thanks for your help.



Damien McBain[_2_]

Error Handling problem
 
"Brassman" wrote in message
...
Not sure why excel is doing this... I have this code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/23/2005 by andya
Dim c As OLEObject
For x = 1 To Worksheets.Count
On Error GoTo ErrHand
For Each c In Worksheets(x)
'do stuff
Next c
ErrHand:
Next x

End Sub

This is what happens. When I step through the code the error handling
works
fine when x=1. But on the next trip through (x=2), It gives me Run-time
error '438' on the "For Each c in Worksheets(x)" line and doesn't use the
error handling. Why?? Thanks for your help.

Put your error trapper outside the For ...Next



Tom Ogilvy

Error Handling problem
 
Look in excel VBA help at the resume statement. Until you issue a resume,
you are still in error handling mode and the next error causes excel to
quit.

--
Regards,
Tom Ogilvy


"Brassman" wrote in message
...
Not sure why excel is doing this... I have this code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/23/2005 by andya
Dim c As OLEObject
For x = 1 To Worksheets.Count
On Error GoTo ErrHand
For Each c In Worksheets(x)
'do stuff
Next c
ErrHand:
Next x

End Sub

This is what happens. When I step through the code the error handling

works
fine when x=1. But on the next trip through (x=2), It gives me Run-time
error '438' on the "For Each c in Worksheets(x)" line and doesn't use the
error handling. Why?? Thanks for your help.





Brassman[_5_]

Error Handling problem
 
Thanks. I had an error in my code too. It should have been:

For Each c In Worksheets(x).OLEObjects
instead of
For Each c In Worksheets(x)


"Tom Ogilvy" wrote:

Look in excel VBA help at the resume statement. Until you issue a resume,
you are still in error handling mode and the next error causes excel to
quit.

--
Regards,
Tom Ogilvy


"Brassman" wrote in message
...
Not sure why excel is doing this... I have this code:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/23/2005 by andya
Dim c As OLEObject
For x = 1 To Worksheets.Count
On Error GoTo ErrHand
For Each c In Worksheets(x)
'do stuff
Next c
ErrHand:
Next x

End Sub

This is what happens. When I step through the code the error handling

works
fine when x=1. But on the next trip through (x=2), It gives me Run-time
error '438' on the "For Each c in Worksheets(x)" line and doesn't use the
error handling. Why?? Thanks for your help.







All times are GMT +1. The time now is 10:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com