How to Search the Sheet Name with VBA in Excel – 3 Examples

This is the sample dataset.

VBA to Search Sheet Name in Excel and Display Search Result


Example 1 – Using Excel VBA to Search the Sheet Name and Display the Search Result

 

VBA to Search Sheet Name in Excel and Display Search Result

STEPS:

  • Go to the Developer tab and select Visual Basic. You can also press Alt + F11 to open the Visual Basic window.

VBA to Search Sheet Name in Excel and Display Search Result

  • Select Insert.
  • Choose Module.

VBA to Search Sheet Name in Excel and Display Search Result

  • Enter 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

ShtName and ShtSearch are declared as variables. ShtName denotes the sheet name and ShtSearch searches the sheet name. The On Error Resume Next statement is used to run the next statements after the statement that generated the error and the On Error GoTo 0 statement is used to disable any enabled error.

  • Press Ctrl + S to save the code.
  • Close the Visual Basic window.
  • Select Macros in the Developer tab. The Macro window will be displayed.

VBA to Search Sheet Name in Excel and Display Search Result

  • Select the macro and click Run.

VBA to Search Sheet Name in Excel and Display Search Result

  • In the message box, enter the sheet name.
  • Click OK.

VBA to Search Sheet Name in Excel and Display Search Result

Another message box will display the search results:

VBA to Search Sheet Name in Excel and Display Search Result

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


Example 2 – Search the Sheet Name and Select it with VBA in Excel

STEPS:

  • Go to the Developer tab and select Visual Basic.

Search Sheet Name and Select it in Excel with VBA

  • Select Insert.
  • Choose Module.

Search Sheet Name and Select it in Excel with VBA

  • Enter 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

The ShtSearch function checks the existence of the searched sheet in an open workbook. ShtName = vbNullString displays Cancelled! if we close the message box.

  • Press Ctrl + S to save the code.
  • Close the Visual Basic window.
  • Select Macros in the Developer tab. The Macro window will be displayed.

Search Sheet Name and Select it in Excel with VBA

  • Select the macro and click Run.

Search Sheet Name and Select it in Excel with VBA

  • Enter the sheet name in the message box and click OK.

Search Sheet Name and Select it in Excel with VBA

If the sheet is found in the open workbook, it will be selected.

Search Sheet Name and Select it in Excel with VBA

Otherwise, the message: Sheet name could not be found! is displayed.

Search Sheet Name and Select it in Excel with VBA


Example 3 – Using Excel VBA to Search the Sheet Name in a Closed Workbook and Display the Search Result

STEPS:

  • Go to the Developer tab and select Visual Basic.

  • Select Insert.
  • Choose Module.

  • Enter the 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

The Workbooks.Open() command opens the closed workbook. Workbooks.Open (“D:\Excel Closed Workbook\Hyperlink PDF files.xlsx”) is the address of the closed sheet.

  • Press Ctrl + S to save the code.
  • Close the Visual Basic window.
  • Select Macros in the Developer tab. The Macro window will be displayed.

  • Select the macro and click Run.

  • Enter the name of the sheet and click OK.

You will see the following message:

Read More: How to Rename Sheet with VBA in Excel


Things to Remember

  • You can also press F5 to run the code.

Download Practice Book

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