Check if worksheet exists

jlepard

New Member
Joined
Apr 10, 2002
Messages
5
Trying to add VBA code to check if a worksheet ("Data") exists in the workbook. Have tried If Not SheetExists("Data") Then and other configurations, but none seem to work.

Appreciate any suggestions.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:

Sub findsheet()
sheetfound = False
For Each sht In Worksheets
If sht.Name = "Data" Then
sheetfound = True
Exit For
End If
Next
End Sub
 
Upvote 0
Another way besides Rickys

<pre/>

Dim sh As Worksheet

On Error Resume Next
Set sh = ThisWorkbook.Sheets("Sheet1")
If sh Is Nothing Then MsgBox "Sheet doesn't exist!"
Set sh = Nothing: On Error GoTo 0

'// Rest of your code
'// goes here

</pre>
 
Upvote 0
Ricky,

I tried to add an ElseIf in case the "Data" sheet is not found, but condition fails. Also tried to change condition to <> "Data" but that also failed. Any suggestions on how to add an Elseif the sht.name is not "Data"?

Thanks for any assistance.
 
Upvote 0
Ricky,

Apologies for previous message. Discovered I have to add the If condition after Next and test if Sheetfound = True or False.
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,025
Members
448,543
Latest member
MartinLarkin

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