Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default 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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error handling with a handling routine ben Excel Programming 0 March 15th 05 03:01 PM
error handling jeffP Excel Programming 1 July 3rd 04 06:10 PM
Error Handling Todd Excel Programming 1 February 13th 04 11:29 PM
Error handling Tim C Excel Programming 1 October 7th 03 10:00 PM
Error Handling Rob Bovey Excel Programming 0 August 7th 03 12:11 AM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"