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

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
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
 
Upvote 0
Thanks, but I should have mentioned the tab name can be changed by the user. Therefore, I need to check by the Sheet #.
 
Upvote 0
Anyone else have any ideas how I can specify a sheet# rather than the name of the sheet? thx
 
Upvote 0
Try the sheet index number....

in the Project = VBAProject dialog
to access use the module process.
 
Upvote 0
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
 
Upvote 0
katie
try
Code:
Sub test()
Dim i As Integer
For i = 1 To Sheets.Count
   MsgBox Sheets(i).CodeName
Next
End Sub
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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
Back
Top