When you’re working with Excel macros, you often need to check whether a specific sheet exists in the workbook before performing any tasks on it. The reason is obvious—if you try to access a sheet that doesn’t exist, your VBA code will show an error and stop running.
That is why it is helpful to have a quick check with Excel VBA whether the sheet exists or not. In today’s article, we will walk you through five practical methods to do so. It includes checking for the sheet in the current workbook, in a closed workbook, checking in another open workbook, asking the user for the sheet name, and creating the sheet if it doesn’t exist.
➤ Press Alt + F11 to open the VBA editor.
➤ Click Insert >> Module in the VBA editor.
➤ Insert the below VBA macro into the module.
Sub CheckSheetInCurrentWorkbook()
Dim sheetName As String
Dim ws As Worksheet
sheetName = "Monthly Budget" ' Change this to the sheet name you want to check
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0
If Not ws Is Nothing Then
MsgBox "Sheet '" & sheetName & "' exists!"
Else
MsgBox "Sheet '" & sheetName & "' does not exist."
End If
End Sub
➤ Run the code.
➤ The result will show a message box confirming whether the specified sheet exists in the current workbook or not.
Check If the Sheet Exists in the Current Workbook
When working with VBA macros, you can quickly check whether a specific sheet exists in your current workbook using this method. After running the VBA, it will show a simple confirmation through a message box letting you know if the sheet is present or not.
Here we have a sample workbook containing three sheets, namely Monthly Budget, Employee Attendance, and Customer Feedback. Now we are going to use a VBA macro and check a specific sheet, like the Monthly Budget, in different scenarios.
Steps:
➤ Press Alt + F11 to open the VBA or Visual Basic for Applications editor.
➤ In the VBA editor menu, click Insert >> Module
➤ Insert the below VBA code into the VBA module. You can copy and paste it.
➤ Before that, replace “Monthly Budget” in the code with the exact name of the sheet you want to check in your workbook.
Sub CheckSheetInCurrentWorkbook()
Dim sheetName As String
Dim ws As Worksheet
sheetName = "Monthly Budget" ' Change this to the sheet name you want to check
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0
If Not ws Is Nothing Then
MsgBox "Sheet '" & sheetName & "' exists!"
Else
MsgBox "Sheet '" & sheetName & "' does not exist."
End If
End Sub
➤ Run the code.
➤ If the sheet exists in the current workbook, for instance, Monthly Budget, a message box will pop up saying Sheet ‘Monthly Budget’ exists!
➤ If it doesn’t exist, you’ll see Sheet ‘Sheet name’ does not exist.
Ask the Sheet Name from the User and Check If It Exists
If you want to make the VBA code more flexible, you can use this method. This method allows the users to type the sheet name, then checks if the sheet exists in the current workbook. It gives you a simple input box to enter any sheet name, and returns a clear message box with the result. It’s useful for shared files or reports where sheet names may change.
Steps:
➤ Press Alt + F11 to open the VBA editor.
➤ Click Insert >> Module
➤ In the new module, insert the following VBA macro. You can just copy and paste it; no changes are required.
Sub CheckSheetFromUserInput()
Dim sheetName As String
Dim ws As Worksheet
' Ask user for the sheet name
sheetName = InputBox("Enter the sheet name you want to check:", "Check Sheet")
'the If user clicks Cancel or enters nothing, exit the sub
If sheetName = "" Then
MsgBox "No sheet name entered. Exiting macro."
Exit Sub
End If
On Error Resume Next
Set ws = ThisWorkbook.Sheets(sheetName)
On Error GoTo 0
If Not ws Is Nothing Then
MsgBox "Sheet '" & sheetName & "' exists!"
Else
MsgBox "Sheet '" & sheetName & "' does not exist."
End If
End Sub
➤ Run the code.
➤ A prompt will appear asking you to type in the name of the sheet you want to check.
➤ Type the name, for example, Monthly Budget >> click OK.
➤ If the sheet exists, you’ll see:
“Sheet ‘Monthly Budget’ exists!”. Otherwise, it will show “Sheet ‘Yoursheetname’ doesn’t exist!”
Check If the Sheet Exists in a Closed Workbook
If you want to check whether a specific sheet exists in another workbook that is closed, this VBA macro can help. You can use the method in case you want to check if the sheet exists without opening that specific workbook fully, or don’t want to make any edits to that workbook. After running the code, you’ll get a quick exists or not exist message based on the sheet’s presence.
Steps:
➤ Open the VBA editor pressing Alt + F11 to open the VBA editor.
➤ Click Insert >> Module
➤ In the new module, copy and paste the following macro.
Sub CheckSheetInClosedWorkbook()
Dim wbPath As String
Dim wb As Workbook
Dim ws As Worksheet
Dim sheetName As String
Dim sheetFound As Boolean
wbPath = "C:\Users\HP USER\Downloads\ClosedWorkbook.xlsx" ' Change to your file path
sheetName = "Monthly Budget" ' Change to the sheet you're looking for
' Check if file exists
If Dir(wbPath) = "" Then
MsgBox "Workbook not found at path: " & wbPath
Exit Sub
End If
' Open the workbook (hidden)
Set wb = Workbooks.Open(Filename:=wbPath, ReadOnly:=True)
sheetFound = False
For Each ws In wb.Sheets
If ws.Name = sheetName Then
sheetFound = True
Exit For
End If
Next ws
' Show result
If sheetFound Then
MsgBox "Sheet '" & sheetName & "' exists in the closed workbook."
Else
MsgBox "Sheet '" & sheetName & "' was NOT found in the closed workbook."
End If
' Close the workbook
wb.Close SaveChanges:=False
End Sub
Note:
➥ In this part of the code, wbPath = “C:\Users\HP USER\Downloads\ClosedWorkbook.xlsx”, change the file path exactly with your own file location, including the xlsx extension.
➥ sheetName = “Monthly Budget”, here, change Monthly Budget with your exact sheet name.
➤ Run the code.
➤ You’ll see a message confirming whether the sheet exists in the closed file. No changes will be made in the closed workbook.
Check Whether a Sheet Exists and Create It If It Doesn’t
The following VBA macro checks whether your specific sheet exists in the current workbook. If the sheet is already there, nothing will change in the workbook. If not, a new sheet with your chosen name will be added to the workbook. This method is helpful when you want to create a new worksheet but are not sure whether the sheet exists or not in the current workbook containing many sheets.
Steps:
➤ Open the VBA editor pressing Alt + F11 to open the VBA editor.
➤ Click Insert >> Module
➤ In the new module, copy and paste the following macro.
➤ Change the sheet name to your desired sheet name. We’ve entered the sheet name as Report2025.
Sub CheckAndCreateSheet()
Dim sheetName As String
Dim ws As Worksheet
Dim sheetExists As Boolean
sheetName = "Report2025" ' Change this to your desired sheet name
sheetExists = False
' Check if the sheet exists
For Each ws In ThisWorkbook.Sheets
If ws.Name = sheetName Then
sheetExists = True
Exit For
End If
Next ws
' Create the sheet if it doesn’t exist
If sheetExists Then
MsgBox "Sheet '" & sheetName & "' already exists."
Else
ThisWorkbook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = sheetName
MsgBox "Sheet '" & sheetName & "' has been created."
End If
End Sub
➤ Run the code.
➤ If it finds one with the name you typed, it shows a message. If not, it shows a message Sheet has been created.
➤ You will see a new blank worksheet at the end of your workbook.
Check If the Sheet Exists in Another Open Workbook
If you are working with multiple Excel files one at a time, this method will help you to check whether the specific sheet exists in another open workbook. It is a helpful method to avoid errors when you need to use data from another workbook. The following will show a message confirming if the sheet exists in the specified open workbook.
Steps:
➤ Open the VBA editor pressing Alt + F11 to open the VBA editor.
➤ Click Insert >> Module
➤ In the new module, copy and paste the following macro.
Sub CheckSheetInAnotherOpenWorkbook()
Dim wbName As String
Dim sheetName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim sheetExists As Boolean
wbName = "ClosedWorkbook.xlsx" ' Change this to the name of the other open workbook (with extension)
sheetName = "Report2025" ' Change this to the sheet you want to check
sheetExists = False
On Error Resume Next
Set wb = Workbooks(wbName)
On Error GoTo 0
If wb Is Nothing Then
MsgBox "Workbook '" & wbName & "' is not open."
Exit Sub
End If
For Each ws In wb.Sheets
If ws.Name = sheetName Then
sheetExists = True
Exit For
End If
Next ws
If sheetExists Then
MsgBox "Sheet '" & sheetName & "' exists in workbook '" & wbName & "'."
Else
MsgBox "Sheet '" & sheetName & "' does NOT exist in workbook '" & wbName & "'."
End If
End Sub
Note:
➥ Change wbName to the exact file name of the other open workbook you want to check, including .xlsx extension.
➥ Change sheetName to the sheet you want to find.
➤ Run the code.
➤ If the other workbook is open and has the sheet, a message box will confirm it. If it is otherwise, you’ll get a clear message telling you.
Frequently Asked Questions
How do I add a new Sheet in Excel VBA?
➤ Press Alt + F11 to open the VBA editor.
➤ Go to Insert >> Module
➤ Insert the following VBA macro in the module.
Sub AddNewSheet()
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "NewSheet"
End Sub
➤ Run the code.
➤ It will add a new sheet at the end of the workbook named NewSheet.
What is the VBA function to check if a file exists?
➤ Open the VBA editor by pressing Alt + F11
➤ Go to Insert >> Module
➤ Insert the following VBA macro in the module.
Function FileExists(filePath As String) As Boolean
If Dir(filePath) <> "" Then
FileExists = True
Else
FileExists = False
End If
End Function
➤ Run the code
➤ The code will return True if the file exists, otherwise False.
How do you check if a worksheet name exists in Excel?
➤ Press Alt + F11 to open the VBA editor.
➤ Go to Insert >> Module
➤ Copy and paste the following VBA macro in the module.
Function SheetExists(sheetName As String) As Boolean
Dim ws As Worksheet
SheetExists = False
For Each ws In ThisWorkbook.Sheets
If ws.Name = sheetName Then
SheetExists = True
Exit Function
End If
Next ws
End Function
➤ Run the code
➤ The code will return True if the sheet exists, otherwise False.
Wrapping Up
In today’s guide, we have learnt how to check if a sheet exists with Excel VBA in five different scenarios. These VBA codes effectively help you identify the existence of the sheet in the current workbook, or even in closed or other open workbooks. Feel free to download the sample workbook and try out the VBA macros yourself. Let us know how these codes have simplified your Excel tasks.