Check if sheet exists

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Just want to add to my code a line to check whether the sheet exists and if not then exit sub


The sheet would be refererred to as

Sheet(P)

So something that essentially says

Code:
If Not Sheet(P) Exists Then
Exit Sub
Else
'Code
End If
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code:
[COLOR="Blue"]Function[/COLOR] SheetExists(SheetName [COLOR="Blue"]As[/COLOR] String) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] sh [COLOR="Blue"]As[/COLOR] Worksheet
    [COLOR="Blue"]On[/COLOR] [COLOR="Blue"]Error[/COLOR] [COLOR="Blue"]Resume[/COLOR] [COLOR="Blue"]Next[/COLOR]
    [COLOR="Blue"]Set[/COLOR] sh = Worksheets(sh)
    [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] sh [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR] SheetExists = [COLOR="Blue"]True[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
 
Upvote 0
Assuming that P is a variable holding the sheet name try

Code:
If Not WorksheetExists(P) Then
    Exit Sub
Else
'Code
End If



Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
End Function
 
Upvote 0
Sub Foo()
If Not SheetExists("P") Then
Exit Sub
Else
MsgBox "Sheet(P) Exists"
End If
End Sub


Function SheetExists(SheetName As String) As Boolean
Dim sh As Worksheet
On Error Resume Next
Set sh = Worksheets(SheetName)
If Not sh Is Nothing Then SheetExists = True
End Function
 
Upvote 0
Works perfect thanks.

VoG, I'm not very experienced in creating my own functions yet, could you just explain how you code works a little bit please so I can learn.
 
Upvote 0
WorksheetExists is False by default.

Code:
WorksheetExists = Worksheets(WSName).Name = WSName
If the sheet exists this will return True.

If the sheet doesn't exist that line of code will error (which is suppressed by On Error Resume Next) so result is False (the default).
 
Upvote 0
Thought that was how it worked, with it being Boolean.

Thanks for the solution and explanation
 
Upvote 0

Forum statistics

Threads
1,224,569
Messages
6,179,605
Members
452,928
Latest member
VinceG

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