How to Search Sheet Name with VBA in Excel (3 Examples)

In this article, we will learn to search the sheet name with VBA in Excel. VBA stands for Visual Basic for Applications. Sometimes, our Excel sheet contains a large number of sheets and we need to search for a specific sheet. It is very time-consuming if we try to search the sheet name manually. So, today, we will use VBA to search the sheet name in Excel. In this article, we are going to demonstrate 3 important VBA examples.


How to Search Sheet Name with VBA in Excel: 3 Examples

To explain these examples, we will use a dataset that contains information about the Department and the Age of some employees.

VBA to Search Sheet Name in Excel and Display Search Result


1. Excel VBA to Search Sheet Name and Display Search Result

In the first example, we will use VBA to search the sheet name. Our desired sheet name is Display Result. The workbook contains many other different sheets along with the desired one. In this case, we will search the sheet name and display the result in a message box.

VBA to Search Sheet Name in Excel and Display Search Result

Let’s follow the steps below to learn this example.

STEPS:

  • Go to the Developer tab and select Visual Basic. The Visual Basic window will appear.
  • Alternatively, you can use the keyboard shortcut for it. Press Alt + F11 to open the Visual Basic window.

VBA to Search Sheet Name in Excel and Display Search Result

  • Next, select Insert.
  • Then click on Module from the drop-down menu in the Visual Basic window. This will open a Module window.

VBA to Search Sheet Name in Excel and Display Search Result

  • Type the code in the Module window.
Sub Search_Excel_Sheet_Name()
Dim ShtName As String
Dim ShtSearch As Boolean
ShtName = InputBox("Enter the Sheet Name:")
If ShtName = "" Then Exit Sub
On Error Resume Next
ActiveWorkbook.Sheets(ShtName).Select
ShtSearch = (Err = 0)
On Error GoTo 0
If ShtSearch Then
MsgBox "Sheet '" & ShtName & "' has been found!"
Else
MsgBox "Sheet '" & ShtName & "' could not be found!"
End If
End Sub

VBA to Search Sheet Name in Excel and Display Search Result

Here, we have declared ShtName and ShtSearch as variables. ShtName denotes the sheet name and ShtSearch searches the sheet name. We have used the On Error Resume Next statement to run the next statements instantly after the statement that generated the error and the On Error GoTo 0 statement to disable any enabled error.

  • Press Ctrl + S to save the code.
  • Then, close the Visual Basic window.
  • Now, select Macros from the Developer tab. The Macro window will appear.

VBA to Search Sheet Name in Excel and Display Search Result

  • Now, select the code and Run it from the Macro window.

VBA to Search Sheet Name in Excel and Display Search Result

  • After running the code, a message box will ask for the sheet name.
  • Write the sheet name and click OK to proceed.

VBA to Search Sheet Name in Excel and Display Search Result

  • Finally, another message box will display the search results like the picture below.

VBA to Search Sheet Name in Excel and Display Search Result

Read More: How to Select Sheet by Variable Name with VBA in Excel


2. Search Sheet Name and Select it with VBA in Excel

In this example, we will search the sheet name and select it with Excel VBA. Here, we will not show the result in a message box. Rather, we will select the searched sheet. Let’s pay attention to the steps below.

STEPS:

  • To begin with, select Visual Basic from the Developer tab. This will open the Visual Basic window.

Search Sheet Name and Select it in Excel with VBA

  • Click on Module from the Insert tab. The Module window will appear.

Search Sheet Name and Select it in Excel with VBA

  • After that, type the code in the Module window.
Sub Search_and_Select_Sheet()
Dim ShtName As String
ShtName = InputBox("Enter the sheet name:")
If ShtName = vbNullString Then
MsgBox "Cancelled!"
Exit Sub
End If
If ShtSearch(ShtName) Then
Worksheets(ShtName).Activate
Else
MsgBox "Sheet name could not be found!"
End If
End Sub
Function ShtSearch(ShtName As String) As Boolean
Dim wks As Worksheet
On Error Resume Next
Set wks = Worksheets(ShtName)
If Not wks Is Nothing Then ShtSearch = True
End Function

Search Sheet Name and Select it in Excel with VBA

Here, we have created the ShtSearch function to check the existence of the searched sheet in an open workbook. We have used the ShtName = vbNullString command to show Cancelled! if we close the message box.

  • Next, press Ctrl + S to save the code. Then, close the Visual Basic window.
  • Go to the Developer tab and select Macros to open the Macro window.

Search Sheet Name and Select it in Excel with VBA

  • Then, select the desired macro and Run it from the Macro window.

Search Sheet Name and Select it in Excel with VBA

  • After running the code, you have to write the sheet name in the message box and click OK.

Search Sheet Name and Select it in Excel with VBA

  • Finally, if it finds the sheet in the open workbook, it will select the sheet.

Search Sheet Name and Select it in Excel with VBA

  • Otherwise, it will display the Sheet name could not be found! message.

Search Sheet Name and Select it in Excel with VBA


3. Excel VBA to Search Sheet Name in Closed Workbook and Display Search Result

In the last example, we will search for a sheet name in a closed workbook with VBA. Here, we will use a workbook that contains a sheet named Insert Tab. We will search for this sheet name and display the result in a message box.

Let’s observe the steps below to learn more about this example.

STEPS:

  • Go to the Developer tab and select Visual Basic. This will open the Visual Basic window.

  • Click on the Insert tab. Then select Module from the drop-down menu to open the Module window.

  • Then, type the VBA code in the Module window.
Sub Search_Sheet_Name_in_Closed_Workbook()
Dim xWkb As Workbook
Dim xSht As Worksheet
Dim xShtName As String
xShtName = InputBox("Enter the Sheet Name:")
Application.ScreenUpdating = False
Set xWkb = Workbooks.Open _
("D:\Excel Closed Workbook\Hyperlink PDF files.xlsx")
For Each xSht In xWkb.Worksheets
If xSht.Name = xShtName Then
xWkb.Close SaveChanges:=True
MsgBox "Sheet '" & xShtName & "' has been found!"
Exit Sub
End If
Next xSht
Application.ScreenUpdating = False
MsgBox "Sheet '" & xShtName & "' could not be found!"
End Sub

Here, we have used the Workbooks.Open() command to open the closed workbook. Workbooks.Open (“D:\Excel Closed Workbook\Hyperlink PDF files.xlsx”) denotes the address location of the closed sheet. It means the VBA code will have to open the file located on the D drive inside the Excel Closed Workbook file and the name of the file is Hyperlink PDF files. We will then have to write the extension of the file. As it is an Excel workbook, we have written .xlsx.

  • Press Ctrl + S to save the code and close the Visual Basic window.
  • Now, go to the Developer tab and select Macros. It will open the Macro window.

  • Select the desired code from the Macro window and Run it.

  • After that, a message box will appear. Type the name of the sheet you want to search in the closed workbook and click OK.

  • Finally, you will see a message like the below picture if the searched name exists in the closed workbook.

Read More: How to Rename Sheet with VBA in Excel


Things to Remember

There are certain things we need to remember to search the sheet name with VBA in Excel.

  • In the above methods, we need to write the sheet name fully in the message box. For example, if you write Display instead of Display Result, it will show Sheet ‘Display’ could not be found!
  • You can press Alt + F11 to open the Visual Basic window.
  • Again, you can press the F5 key to run the code instead of running it from the Macro window.

Download Practice Book


Conclusion

We have demonstrated 3 easy examples to search a sheet name in Excel with VBA. I hope these examples will help you to perform your tasks easily. Furthermore, we have also added the practice book at the beginning of the article. You can download it to learn more. Last of all, if you have any suggestions or queries, feel free to ask in the comment section below.

Get FREE Advanced Excel Exercises with Solutions!
Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo