VBA Code to Check if Sheet Exists

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance, I will try to give feedback for any potential solutions given.

This code was working, but I now get error "Compile error: Method or Member Data not Found"

Code:
Sub CheckSht()

 Dim FirstDate As String
 Dim Sheet As Sheets
 
 
    '_____________________________________________________________________________________________
    'Make a copy of "Sheet1" sheet.
        'Get the date of the first one
                Sheets("Sheet1").Activate
                FirstDate = Format(Range("E2").Value, "YYYY.MM.DD")
        
        'Check to see if the sheet exists and if so, terminate the program
            'If ActiveSheet.Name = "Sheet1" & "." & FirstDate
                For Each Sheet In ActiveWorkbook.Worksheets
                    
                    If Sheet.Name = "Sheet1" & "." & FirstDate Then
                        MsgBox "The Worksheet " & """Sheet1." & FirstDate & """ already exists!"
                        Exit Sub
                        
                    ElseIf Sheet.Name = "Sheet2" & "." & FirstDate Then
                        MsgBox "The Worksheet " & """Sheet2." & FirstDate & """ already exists!"
                        Exit Sub
                        
                    End If
                    
                Next Sheets


        'Make a copy of the sheet and name it
            Sheets("Sheet1").Copy After:=Sheets("Sheet1")
            ActiveSheet.Name = "Downloaded.EDDs" & "." & FirstDate


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here's a function you can call from a subroutine as in the example below:
VBA Code:
Function SheetExists(wbName As String, shName As String) As Boolean
SheetExists = False
With Workbooks(wbName)
    For Each sh In .Sheets
        If sh.Name = shName Then
            SheetExists = True
            Exit For
        End If
     Next sh
End With
End Function
Call function from a subroutine:
VBA Code:
Sub CheckIfSheetExists()
' see if a sheet named mySht exists in the activeworkbook
Dim mySht
If SheetExists(ActiveWorkbook.Name, "mySht") Then
    MsgBox "mySht is a sheet in the activeworkbook"
Else
    MsgBox "mySht is not a sheet in the activeworkbook"
End If
End Sub
 
Upvote 0
Here is another option you could try in a copy of your workbook.
BTW, should the code also check that sheet "Downloaded.EDDs." & FirstDate does not already exist?

VBA Code:
Sub Test()
  Dim FirstDate As String
 
  FirstDate = Format(Sheets("Sheet1").Range("E2").Value, "yyyy.mm.dd")
  If ShtExists("Sheet1." & FirstDate) Then
    MsgBox "The Worksheet " & """Sheet1." & FirstDate & """ already exists!"
  ElseIf ShtExists("Sheet2." & FirstDate) Then
    MsgBox "The Worksheet " & """Sheet2." & FirstDate & """ already exists!"
  Else
    Sheets("Sheet1").Copy After:=Sheets("Sheet1")
    Sheets(Sheets("Sheet1").Index + 1).Name = "Downloaded.EDDs." & FirstDate
  End If
End Sub


Function ShtExists(SheetName As String, Optional wb As Workbook) As Boolean
  On Error Resume Next
  ShtExists = (IIf(wb Is Nothing, ActiveWorkbook, wb).Sheets(SheetName).Name <> "")
  On Error GoTo 0
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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