If you want to create a sheet, want to delete it, or move or copy it, there’s one thing that you need to know if that sheet exists or not.
To write code to check whether the sheet exists or not you need a loop that loops through each sheet in the workbook and matches the name you have provided. But here’s the thing, you can use two different loops for this (For Next and For Each), and today we will use both.
In this tutorial, we will look at different ways to do that, so, make sure to have the developer tab on your ribbon and open the VBA editor to write this code.
Check IF a Sheet Exists in the Current Workbook
With this loop, you can refer to all the sheets in the workbook and loop through each one by one to match the name of the sheet with the sheet name that you want to search for.
Follow these steps:
- First, declare a variable to use for the sheet while performing the loop and to store the sheet name that you want to search.
- Next, write a line of code for an input box to enter the name of the sheet that you wish to search.
- After that, start your loop with the For Each keyword. And use the variable to refer to each worksheet in the workbook.
- From here, you need to write an IF THEN ELSE statement to match the name of the sheet with the name that you have entered in the input box, and then show a message box if match found and exit the procedure.
- In the end, a message box to inform you if there’s no match found.
Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook
Full Code:
Sub vba_check_sheet()
Dim sht As Worksheet
Dim shtName As String
shtName = InputBox(Prompt:="Enter the sheet name", _
Title:="Search Sheet")
For Each sht In ThisWorkbook.Worksheets
If sht.Name = shtName Then
MsgBox "Yes! " & shtName & " is there in the workbook."
Exit Sub
End If
Next sht
MsgBox "No! " & shtName & "is not there in the workbook."
End Sub
Let me explain how this works: When you run this code, it shows you a message where you need to enter the sheet name that you wish to find.
After that, it loops through each sheet to match the name with the name you have entered, and if the name matches with a sheet, it shows you a message and another message if there’s no match.
Here is another code to check if a sheet exists or not.
Sub vba_check_sheet()
Dim sht As Worksheet
Dim shtName As String
Dim i As Long
i = Sheets.Count
shtName = InputBox(Prompt:="Enter the sheet name", _
Title:="Search Sheet")
For i = 1 To i
If Sheets(i).Name = shtName Then
MsgBox "Yes! " & shtName & " is there in the workbook."
Exit Sub
End If
Next i
MsgBox "No! " & shtName & " is not there in the workbook."
End Sub
This code uses the FOR NEXT loop and uses the total count of sheets in the workbook, and based on that, perform a loop in each sheet that matches the name with the name you have entered.
Check IF Sheet Exists in Closed Workbook
In the following code, you have a loop that searches for the sheet name in a closed workbook. To refer to the file, we used the file address.
Sub vba_check_sheet()
Dim wb As Workbook
Dim sht As Worksheet
Dim shtName As String
shtName = InputBox(Prompt:="Enter the sheet name", _
Title:="Search Sheet")
Application.ScreenUpdating = False
Set wb = Workbooks.Open _
("C:UsersDellDesktopsample-file.xlsx")
For Each sht In wb.Worksheets
If sht.Name = shtName Then
wb.Close SaveChanges:=True
MsgBox "Yes! " & shtName & " is there in the workbook." _
, vbInformation, "Found"
Exit Sub
End If
Next sht
Application.ScreenUpdating = False
MsgBox "No! " & shtName _
& " is not there in the workbook.", _
vbCritical, "Not Found"
End Sub
When you run this macro, it opens the file at the back end as you have turned OFF the screen updating, and once it loops through all the sheets, you have code to turn ON the screen updating.
Note: As you can see, in the file location address, we have the file extension which means you need to have the correct extension of the file to refer to it.