Sometimes, we have to select a particular sheet from multiple worksheets in the Excel workbook. We can call the sheet by its default Code Name or by using a Variable Name. In order to select a sheet using the variable name, we must set the variable name first. In this article, we will show you the effective yet simple ways to Select a Sheet by using a Variable Name with VBA in Excel.
To illustrate, we will use a sample dataset as an example. For instance, the following dataset represents the Salesman, Product, and Net Sales of a company shown in different sheets.
Download Practice Workbook
Download the following workbook to practice by yourself.
2 Ways to Select Sheet by Variable Name with VBA in Excel
1. Select Active Sheet by Variable Name with VBA in Excel
In our first method, we’ll select the active sheet using a variable name. By Active Sheet, we mean the sheet on which we are working. Therefore, follow the steps below to Select Active Sheet by Variable Name with VBA in Excel.
STEPS:
- First, select Visual Basic under the Developer tab.
- As a result, the VBA window will pop out.
- Then, select Module from the Insert tab.
- Consequently, the Module window will appear.
- There, copy the following code and paste it into the box.
Sub ActiveSheetSelect()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Select
End Sub
- Now, close the VBA window.
- After that, select Macros under the Developer tab.
- As a result, the Macro dialog box will emerge.
- Here, select the ActiveSheetSelect and press Run.
- Finally, it’ll return the sheet that we were working on.
2. Excel VBA to Set Variable Name for Selecting Sheet
In our previous method, we applied the VBA code to return the sheet that we were already working on. In this method, we’ll set a Variable Name for our desired worksheet and select that worksheet using the Variable Name with VBA. So, learn the following process to perform the task.
STEPS:
- Firstly, we’ll set the Variable Name for Sheet2 and use that variable name to select the sheet.
- So, go to Developer ➤ Visual Basic.
- Next, select Insert ➤ Module.
- Hence, the Module dialog box will appear.
- Then, copy the below code and paste it there.
Sub SelectSheet()
Dim VarSheet As String
VarSheet = "Sheet2"
With ActiveWorkbook.VBProject
Worksheets(CStr(.VBComponents(VarSheet).Properties("Name"))).Select
End With
End Sub
- After that, close the VBA window.
- Now, open Sheet3.
- Subsequently, select the Macros from the Developer tab.
- As a result, the Macro dialog box will pop out.
- There, click SelectSheet and press Run.
- After pressing Run, you may get an error dialog box like it’s shown in the following picture.
- To solve the issue, press End.
- Then, go to File ➤ Options.
- After that, from the Trust Center tab, select Trust Center Settings.
- Consequently, the Trust Center dialog box will appear.
- There, select the Macro Settings tab.
- Afterward, check the box for Trust access to the VBA project object model and press OK.
- Again, select Developer ➤ Macros.
- Click SelectSheet and press Run.
- Eventually, it’ll return the Sheet2 even though we were working on Sheet3.
Read More: How to Search Sheet Name with VBA in Excel (3 Examples)
Conclusion
Henceforth, you will be able to Select a Sheet by using the Variable Name with VBA in Excel using the above-described methods. Keep using them and let us know if you have any more ways to do the task. Don’t forget to drop comments, suggestions, or queries if you have any in the comment section below.
Related Articles
- How to Search Sheet Name in Excel Workbook (2 Methods)
- Apply Sheet Name Code in Footer in Excel (3 Ways)
- How to Get Excel Sheet Name (2 Methods)
- List Sheet Name in Excel (5 Methods + VBA)
- How to Rename Sheet in Excel (6 Easy and Quick Methods)
- Rename Sheet with VBA in Excel (Both Single and Multiple Sheets)
- How to Use Excel Sheet Name From Cell Value (Three Ways)