Delete a sheet...VB

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120
Hi,

Need help with an IF statement in VB. I need to check if a Sheet has been deleted before switching to it and processing.

For example, a user could delete a worksheet called out in a macro. If so, the module will fail.

In the code, each sheet is called out by Sheet#.Select. I just want to verify it exists before moving forward.

Thanks,
Katie
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Katie

I may be wrong but i don't think there is a property or a method that tests the existence of a worksheet. However you can trap the error of accessing a property, like in this simple example:

Code:
Sub WshExists()
Dim sS As String

On Error Resume Next
sS = Worksheets("Crrent Crosstab").Name
If Err Then
    MsgBox "Doesn't exist"
Else
    MsgBox "Exists"
End If
End Sub

Hope this helps
PGC
 

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120
Thanks, but I should have mentioned the tab name can be changed by the user. Therefore, I need to check by the Sheet #.
 

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120
Anyone else have any ideas how I can specify a sheet# rather than the name of the sheet? thx
 

Gary_E

Board Regular
Joined
Feb 18, 2002
Messages
108

ADVERTISEMENT

Try the sheet index number....

in the Project = VBAProject dialog
to access use the module process.
 

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120
Gary, :eek: I don't understand what you said...I'm quite the novice. I record macros & edit
 

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120

ADVERTISEMENT

Here is a high level example of how I am calling out the sheets. If sheet6 does not exist because the user deleted it before running the macro, the procedure bombs out.

Sheet6.Select
Cells.Select
Selection.EntireColumn.Hidden = False

Sheet7.Select
Cells.Select
Selection.EntireColumn.Hidden = False
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
katie
try
Code:
Sub test()
Dim i As Integer
For i = 1 To Sheets.Count
   MsgBox Sheets(i).CodeName
Next
End Sub
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi again

Now that you posted your example I don't see why you don't use the error trapping I've posted before.

In your case

Code:
On Error Resume Next

....... code for other sheets

Sheet6.Select
If Err.Number = 0 Then
    Cells.Select
    Selection.EntireColumn.Hidden = False
Else
    Err.Clear
End If

Sheet7.Select
If Err.Number = 0 Then
    Cells.Select
    Selection.EntireColumn.Hidden = False
Else
    Err.Clear
End If

......

Hope this helps
PGC
 

katie1071

Board Regular
Joined
Mar 17, 2003
Messages
120
By using the code provided, will this prevent other errors from occuring? Meaning if other code is called between Sheet6 & Sheet7 below, will the errors still be captured?

On Error Resume Next
....... code for other sheets

Sheet6.Select
If Err.Number = 0 Then
Cells.Select
Selection.EntireColumn.Hidden = False
Else
Err.Clear
End If
*****************************other code
Sheet7.Select
If Err.Number = 0 Then
Cells.Select
Selection.EntireColumn.Hidden = False
Else
Err.Clear
End If
 

Forum statistics

Threads
1,141,097
Messages
5,704,313
Members
421,338
Latest member
Pepess

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top