ExcelBanter

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

Don Rouse

Error Handling
 
We are inserted selected sheets into a workbook. Some of the sheets are to
reference others if the others have been inserted. I have some code that is
supposed to skip to a label if a referenced sheet has not been added to the
workbook.

It does not work. Can you tell me why?

When testing the code, the Steel sheet is present and the Joists and
Deck Erect sheets are not. The code works fine until it tries to select
the Deck Erect sheet. The program stops with Subscript out of range.

I have tried Resume 295 (which is the next line of code) but get a
Compile Error Label not defined.


Sub SetErectRecapRanges()

Sheets("Erect Recap").Select

< some code to name ranges

On Error GoTo SkipSteel
Sheets("Steel").Select If sheet Steel does not exist, code is to
resume at SkipSteel:
Sheets("Erect Recap").Select

< some code to reference the Steel sheet in the Erect Recap sheet

SkipSteel:
On Error GoTo 0
On Error GoTo SkipJoists
Sheets("Joists").Select If sheet Joists does not exist, code is
to resume at SkipJoists:
Sheets("Erect Recap").Select

< some code to reference the Joists sheet in the Erect Recap sheet

SkipJoists:
On Error GoTo 0
On Error GoTo SkipDeck
Sheets("Deck Erect").Select If sheet Deck Erect does not exist,
code is to resume at SkipDeck:
Sheets("Erect Recap").Select

< some code to reference the Deck Erect sheet in the Erect Recap sheet

SkipDeck:
On Error GoTo 0
Range("A1").Select
End Sub


Your assistance is appreciated.

Thank you.
--
Don

Jim Rech

Error Handling
 
You need to do a little reading on how error trapping works. The basic
problem you have is that when you go to an error handler you must Resume out
of it. You cannot set an error handler from within an error hander. This
article may help made that clear:

http://support.microsoft.com/default...b;en-us;141571

But I think it would be better if you tested for a sheet's existence via a
subroutine so that all your error trapping can go there. Use the function
SheetExists to see if the passed worksheet exists and then select it if it
does.

Sub Demo()
If SheetExists("Sheet1") Then
Worksheets("Sheet1").Select
ElseIf SheetExists("Sheet2") Then
Worksheets("Sheet2").Select
End If
End Sub

Function SheetExists(Sheetname As String) As Boolean
On Error Resume Next
SheetExists = (Worksheets(Sheetname).Name < "")
End Function

--
Jim
"Don Rouse" wrote in message
...
| We are inserted selected sheets into a workbook. Some of the sheets are
to
| reference others if the others have been inserted. I have some code that
is
| supposed to skip to a label if a referenced sheet has not been added to
the
| workbook.
|
| It does not work. Can you tell me why?
|
| When testing the code, the "Steel" sheet is present and the "Joists" and
| "Deck Erect" sheets are not. The code works fine until it tries to select
| the "Deck Erect" sheet. The program stops with "Subscript out of range".
|
| I have tried "Resume 295" (which is the next line of code) but get a
| Compile Error "Label not defined".
|
|
| Sub SetErectRecapRanges()
|
| Sheets("Erect Recap").Select
|
| '< some code to name ranges
|
| On Error GoTo SkipSteel
| Sheets("Steel").Select 'If sheet "Steel" does not exist, code is
to
| resume at "SkipSteel:"
| Sheets("Erect Recap").Select
|
| '< some code to reference the "Steel" sheet in the "Erect Recap" sheet
|
| SkipSteel:
| On Error GoTo 0
| On Error GoTo SkipJoists
| Sheets("Joists").Select 'If sheet "Joists" does not exist, code is
| to resume at "SkipJoists:"
| Sheets("Erect Recap").Select
|
| '< some code to reference the "Joists" sheet in the "Erect Recap" sheet
|
| SkipJoists:
| On Error GoTo 0
| On Error GoTo SkipDeck
| Sheets("Deck Erect").Select 'If sheet "Deck Erect" does not exist,
| code is to resume at "SkipDeck:"
| Sheets("Erect Recap").Select
|
| '< some code to reference the "Deck Erect" sheet in the "Erect Recap"
sheet
|
| SkipDeck:
| On Error GoTo 0
| Range("A1").Select
| End Sub
|
|
| Your assistance is appreciated.
|
| Thank you.
| --
| Don




All times are GMT +1. The time now is 04:18 PM.

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