Excel VBA: Check If a Sheet Exists (2 Simple Methods)

One of the many things that we can accomplish using VBA in Excel is that we can check if a particular sheet exists in a workbook or not. In this article, I’ll show you how you can check if a single sheet or a number of sheets exist using Excel VBA.


Excel VBA: Check If a Sheet Exists (Quick View)

Sub Check_If_a_Sheet_Exists()

Workbook_Name = "Check If a Sheet Exists.xlsm"
Sheet_Name = "Sheet1"

Count = 0
For i = 1 To Workbooks(Workbook_Name).Sheets.Count
    If Sheets(i).Name = Sheet_Name Then
        Count = Count + 1
        Exit For
    End If
Next i

If Count > 0 Then
    MsgBox "The Sheet Exists."
Else
    MsgBox "The Sheet doesn't Exist."
End If

End Sub

VBA Code to Check If a Sheet Exits in Excel


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


Developing Macros to Check If a Sheet Exists or Not Using Excel VBA

Without further delay, let’s move to our main discussion today. Our objective is to develop an Excel Macro to check whether a particular sheet exists or not using Excel VBA. We’ll divide the session into two groups. First, we’ll check for a single worksheet, then for a number of worksheets.


1. Developing a Macro to Check If a Single Sheet Exists or Not

First, we’ll learn to develop a Macro to check whether a single sheet exists or not. For example, let’s check whether Sheet1 exists or not in this workbook (Check If a Sheet Exists.xlsx).

⧪ Step 1: Opening the Visual Basic Window

Press ALT + F8 to open the Visual Basic window.

Opening VBA Window to Check If a Sheet Exists or Not Using Excel VBA

⧪ Step 2: Inserting a New Module

Go to the Insert > Module option in the toolbar. Click on Module. A new Module called Module1 will open.

Inserting New Module to Check If a Sheet Exists or Not Using Excel VBA

⧪ Step 3: Putting the VBA Code

This is the time for putting the VBA code. It is already available at the top of the article. Here I’m providing a step-by-step breakdown of the code for easy understanding.

➽ Section 1: Inserting the Necessary Inputs

First of all, we have to insert the necessary inputs into the code. These include the name of the workbook (Check If a Sheet Exists.xlsx) and the worksheet (Sheet1).

Workbook_Name = "Check If a Sheet Exists.xlsm"
Sheet_Name = "Sheet1"

➽ Section 2: Iterating through All the Sheets to Check Whether the Given Name Matches a Sheet or Not
Next, we’ll iterate through a for-loop to check whether the name of the sheet matches any sheet of the workbook or not. We’ll use a variable called Count for this purpose. If any sheet does match, the value of Count will increment from 0 to 1.

Count = 0
For i = 1 To Workbooks(Workbook_Name).Sheets.Count
    If Sheets(i).Name = Sheet_Name Then
        Count = Count + 1
        Exit For
    End If
Next i

➽ Section 3: Showing the Output

Finally, it’s the time for the output. If the value of Count is more than 1, it means the Sheet exists. And if it’s equal to 0, it means the sheet doesn’t exist.

If Count > 0 Then
    MsgBox "The Sheet Exists."
Else
    MsgBox "The Sheet doesn't Exist."
End If

Therefore, the complete VBA code will be:

⧭ VBA Code:

Sub Check_If_a_Sheet_Exists()

Workbook_Name = "Check If a Sheet Exists.xlsm"
Sheet_Name = "Sheet1"

Count = 0
For i = 1 To Workbooks(Workbook_Name).Sheets.Count
    If Sheets(i).Name = Sheet_Name Then
        Count = Count + 1
        Exit For
    End If
Next i

If Count > 0 Then
    MsgBox "The Sheet Exists."
Else
    MsgBox "The Sheet doesn't Exist."
End If

End Sub

VBA Code to Check If a Sheet Exits in Excel

⧪ Step 4: Running the Code

Now the final step, that’s to run the code. For running the code, again go to the Visual Basic toolbar and click on the button Run Sub \ Userform.

As sheet Sheet1 exists on the workbook, a message box will display “The Sheet Exists.”.

Output to Check If a Sheet Exists or Not Using Excel VBA

If it hadn’t existed, the message box would display “The Sheet doesn’t Exist.”.

Read More: Excel VBA: Check If a File Exists or Not


2. Embedding a Macro to Check If Multiple Sheets Exist or Not

We’ve developed a Macro to check for a single sheet. Now we’ll try to develop it for multiple sheets.

For example, let’s check if Sheet1, Sheet2, and Sheet4 exist in the workbook “Check If a Sheet Exists.xlsm” or not.

The steps are all same as the steps mentioned in section 1, except for the VBA code.

Now, can you guess the code?

You can try of your own to develop and run one. In case you can’t, I am providing my code here. Of course, you can change and modify it on your own.

⧭ VBA Code:

Sub Check_If_a_Sheet_Exists()

Workbook_Name = "Check If a Sheet Exists.xlsm"
Sheet_Names = Array("Sheet1", "Sheet2", "Sheet3")

For i = LBound(Sheet_Names) To UBound(Sheet_Names)

    Count = 0
    For j = 1 To Workbooks(Workbook_Name).Sheets.Count
        If Sheets(j).Name = Sheet_Names(i) Then
            Count = Count + 1
        Exit For
        End If
    Next j

    If Count > 0 Then
        Output = Output + Sheet_Names(i) + " Exists." + vbNewLine + vbNewLine
    Else
        Output = Output + Sheet_Names(i) + " doesn't Exist." + vbNewLine + vbNewLine
    End If
Next i

MsgBox Output

End Sub

⧭ Output:

Run this code. It’ll display Sheet1 and Sheet2 exists, and Sheet3 doesn’t exist.

Read More: How to Check If Value Exists in Range in Excel (8 Ways)


Things to Remember

We’ve specified the names of the workbooks within which we are searching for the worksheets. But if you want to search within the active workbook only, you can use the ActiveWorkbook or ThisWorkbook property of VBA.

In fact, if you don’t mention the name of any workbook, directly mention the names of the worksheets, that’ll also do. Excel will automatically work upon the active workbook.


Conclusion

So, these are the ways to check if a sheet exists or not using VBA in Excel. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.


Related Articles

Tags:

Rifat Hassan

Rifat Hassan

Hello! Welcome to my profile. Here I will be posting articles related to Microsoft Excel. I am a passionate Electrical Engineer holding a Bachelor’s degree in Electrical and Electronic Engineering from the Bangladesh University of Engineering and Technology. Besides academic studies, I always love to keep pace with the revolution in technology that the world is rushing towards day by day. I am diligent, career-oriented, and ready to cherish knowledge throughout my life.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo