How to Select Sheet by Variable Name with VBA in Excel (2 Ways)

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.

excel vba select sheet variable name

Read More: How to Use Sheet Name Code in Excel (4 Applications)


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.

Select Active Sheet by Variable Name with VBA in Excel

  • As a result, the VBA window will pop out.
  • Then, select Module from the Insert tab.

Select Active Sheet by Variable Name with VBA in Excel

  • 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

Select Active Sheet by Variable Name with VBA in Excel

  • Now, close the VBA window.
  • After that, select Macros under the Developer tab.

Select Active Sheet by Variable Name with VBA in Excel

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

Excel VBA to Set Variable Name for Selecting 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.

Excel VBA to Set Variable Name for Selecting Sheet

  • As a result, the Macro dialog box will pop out.
  • There, click SelectSheet and press Run.

Excel VBA to Set Variable Name for Selecting Sheet

  • After pressing Run, you may get an error dialog box like it’s shown in the following picture.

Excel VBA to Set Variable Name for Selecting Sheet

  • 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

Aung

Aung

I'm Aung. Recently I've earned my B.Sc. Degree in Electrical and Electronic Engineering. From now on, I will be working in Microsoft Excel and other useful software, and I’ll upload articles related to them. My current goal is to write technical contents for anybody and everybody that will make the learning process of new software and features a happy journey.

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo