How do you check if a sheet exists in Excel VBA?

VBA Code to Check if Sheet with Name exists?

This Excel vba code will check if sheet with a specific name exists in the workbook.

As per Microsoft specifications, You can add as many number of sheets depending on the limits of Your computer’s memory.

So, this loop will check if any of those sheets has the name passed as parameter to this function.

  • There are 2 methods presented in this page.
  • Use option 1 first & Test the performance.
  • If the loop is taking more time, then use 2nd option.

VBA function to Check if Sheet Exists

Here is the vba code the loop thru all the sheets & verifies the name passed as parameter.

Public Function SheetExists(SheetName As String) As Boolean
    'Declare variables - Officetricks.com
    Dim wSh As Worksheet
    Dim bReturnValue As Boolean
    
    'Loop Thru Each Sheet in workbook
    bReturnValue = False
    For Each wSh In ThisWorkbook.Sheets
    
        'Check whether there is a name match
        If VBA.UCase(wSh.Name) = VBA.UCase(SheetName) Then
            bReturnValue = True
            Exit For
        End If
    Next wSh
    
    'Return Match Result
    SheetExists = bReturnValue
End Function

The above function will return ‘True’ if the sheet with exact name exists in the workbook. Otherwise it will return false.

Lets see another version that is more faster than the above one.

VBA Worksheet Exists with specific Name in Workbook

This function does not loop thru all existing sheet. It directly checks for the Sheet name.

If you face any issue in using this function, then use the first one. Option 1 gives good results consistently.

Public Function fSheetExists(SheetName As String) As Boolean
    'Declare variables - Officetricks.com
    Dim wSh As Worksheet
    Dim bReturnValue As Boolean
    Set wSh = Nothing
    
    'Assign Sheet to Object
    On Error Resume Next
    Set wSh = ThisWorkbook.Sheets(SheetName)
    On Error GoTo 0
    
    'Check if Sheet with name exists
    If wSh Is Nothing Then
        bReturnValue = False
    Else
        bReturnValue = True
    End If
    
    'Return Match Result
    fSheetExists = bReturnValue
End Function

Using this function You can confirm is a sheet exists of not.

Then by using the function in here, you can create or add new worksheet to workbook & rename it. Click here to get the code to add new sheet & rename it.

Function to test if excel sheet exists

The function below checks if an excel sheet exists with given name. The function works both with Worksheets and Charts sheets. It is included in Excel VBA under both Worksheet and Chart in module modSheetProcedures which is automatically inserted when the function is selected.

Public Function SheetExists(strSheetName As String, Optional wbWorkbook As Workbook) As Boolean
    If wbWorkbook Is Nothing Then Set wbWorkbook = ActiveWorkbook 'or ThisWorkbook - whichever appropriate
    Dim obj As Object
    On Error GoTo HandleError
    Set obj = wbWorkbook.Sheets(strSheetName)
    SheetExists = True
    Exit Function
HandleError:
    SheetExists = False
End Function

How do you check if a sheet exists in Excel VBA?

Fluff

MrExcel MVP, Moderator

  • #3

Maybe something like

VBA Code:

Dim ShtName As String
ShtName = "Sheet 1"
If Evaluate("isref('" & ShtName & "'!A1)") Then
   'sheet exists do something
Else
   'sheet doesn't exist do something else
End If

  • #4

This is perfect! Thank you again Fluff

How do you check if a sheet exists in Excel VBA?

Fluff

MrExcel MVP, Moderator

  • #5

Glad to help & thanks for the feedback.

  • #6

Maybe something like

VBA Code:

Dim ShtName As String
ShtName = "Sheet 1"
If Evaluate("isref('" & ShtName & "'!A1)") Then
   'sheet exists do something
Else
   'sheet doesn't exist do something else
End If

An additional question: are you able to do a 3rd condition that if the first 2 sheets do not exist do a 3rd code?

How do you check if a sheet exists in Excel VBA?

Fluff

MrExcel MVP, Moderator

  • #7

How about

VBA Code:

Dim ShtName1 As String, ShtName2 As String
ShtName = "Sheet 1"
ShtName = "Sheet 2"
If Evaluate("isref('" & ShtName1 & "'!A1)") Then
   'sheet exists do something
ElseIf Evaluate("isref('" & ShtName2 & "'!A1)") Then
   'sheet doesn't exist do something else
Else
   'whatever
End If

How do you check if a sheet exists in Excel VBA?

Fluff

MrExcel MVP, Moderator

How do you check if a sheet exists in VBA Excel?

What is this? With this code we can use =WorksheetExists(B3) to test any text string to see if it exists as a sheet name in the current workbook.

Is exist in VBA?

Use the VBA Dir function to check if a file exists. The VBA Dir function returns the name of a valid file, so you can use it to test whether a file exists. When the VBA Dir function returns an empty string, it means the file does not exist.

How do I find a specific sheet name in VBA?

Search by worksheet name with VBA.
Press the Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window..
Click Insert > Module, and then paste following VBA code into the new opening Module window..
VBA: Search by worksheet name..
Press the F5 key or click the Run button to run this VBA..

How can I tell if a sheet is active in Excel?

How to quickly show active or selected sheets only in Excel?.
Press Alt + F11 keys to display Microsoft Visual Basic for Applications window..
Then click Insert > Module and paste below VBA to the new Module window. ... .
Click Run button or press F5 key to execute VBA And now only active sheet are shown, others are hidden..