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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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