![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Apr 2002
Posts: 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. |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Mar 2002
Posts: 363
|
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
__________________
It's never too late to learn something new. Ricky |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Another way besides Rickys
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 |
|
|
|
|
|
#4 |
|
New Member
Join Date: Apr 2002
Posts: 5
|
Thanks, Ricky and Ivan. Both suggestions work great.
Appreciate the quick response. |
|
|
|
|
|
#5 |
|
New Member
Join Date: Apr 2002
Posts: 5
|
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. |
|
|
|
|
|
#6 |
|
New Member
Join Date: Apr 2002
Posts: 5
|
Ricky,
Apologies for previous message. Discovered I have to add the If condition after Next and test if Sheetfound = True or False. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|