Home / VBA / How to Activate a Sheet using VBA
Let’s say you are working with multiple worksheets, and for you, it’s hard to navigate to a sheet using a tab. In this situation, you can use a VBA code to activate any worksheet.
And, to write a VBA code for this you need to use Worksheet.Activate Method. In this post, I’d like to share with you a simple way to write this code to activate or select a worksheet. Before you write this code, you need to understand this method.
In this method, you can specify the worksheet’s name or number which you want to activate. Let’s say you need to activate sheet1, then the code will be:
Worksheets("Sheet1").Activate
Or you can use sheet numbers as well.
Worksheets("1").Activate
So the final code will be:
Sub ActivateSheet1()
Worksheets("Sheet1").Activate
End Sub
Examples: Activate a Worksheet with VBA
In the real life, you can use this method in different ways. Here are some of them.
1. Activate a Worksheet on Opening
If you want to activate a specific worksheet every time when you open the workbook then you name that VBA code auto_open.
Sub auto_open()
Worksheets("Sheet1").Activate
End Su
2. Activate a Worksheet and Hide all other
Maybe you want to navigate to a worksheet and hide all the other worksheets from the workbook. You can do this by using the below code.
Sub HideWorksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Change the sheet name from the above code to use it further.
Must Read Next
- VBA to Create New Sheet
- How to Record a Macro in Excel
- VBA Option Explicit
VBA is one of the Advanced Excel Skills, and if you are getting started with VBA, make sure to check out there (What is VBA, and Useful Macro Examples and VBA Codes).
While working in VBA we sometimes refer to another sheet or use another sheet’s properties, suppose we are in sheet 1 working but we want a value from cell A2 in sheet 2, if we refer to sheet 2’s value without activating the sheet first then we will not be able to access the value so to activate a sheet in VBA we use worksheet property as Worksheets(“Sheet2”).Activate.
In Excel, we always work with worksheets. Worksheets have their name to identify better. In regular spreadsheet workings, we directly navigate through shortcut keysAn Excel shortcut is a technique of performing a manual task in a quicker way.read more, or select the sheet by clicking on them. However, in VBA, it is not that easy. First, we need to specify the sheet name we are referring to. Then, we can use the “Select” method to select the sheet.
Table of contents
- Excel VBA Activate Sheet
- What is VBA Activate Method?
- Example #1 – Activate Sheet by its Index Number
- Example #2 – Activate Sheet by its Name
- Example #3 – Activate Sheet from Another Workbook
- Activate Sheet vs. Select Sheet Method
- #1 – Activate Method
- #2 – Select Method
- Recommended Articles
- What is VBA Activate Method?
What is VBA Activate Method?
As the name says, it activates the specified worksheet. To activate the sheet, we need to mention the exact worksheet name using the worksheets object. For example, if you want to activate a ” Sales sheet,” you can use the code below.
Worksheets(“Sales”).Activate
Syntax
So, the syntax of the Activate method is as follows:
Worksheet (“Name of the Sheet”).Activate
Here, a worksheet is the object, and activates are the method.
You can download this VBA Activate Sheet Excel Template here – VBA Activate Sheet Excel Template
Example #1 – Activate Sheet by its Index Number
In Excel, we work with multiple sets of worksheets. Often, we need to move from one sheet to another to get the job done. In VBA, we can use the Activate method to activate the particular Excel sheet.
For example, we have created three sheets: “Sales 2015”, “Sales 2016”, and “Sales 2017.”
We can activate the sheets in two ways. One is by using sheet index numbers, and another is by using the sheet name.
If we want to select the second sheet, we will use the worksheet object and mention the sheet index number as 2.
Code:
Sub Activate_Example1() Worksheets(2).Activate End Sub
When you run the code using the F5 key or manually, this will activate the second sheet, “Sales 2016”.
If we want to activate the third sheet, we will use 3 as the sheet index number.
Code:
Sub Activate_Example1() Worksheets(3).Activate End Sub
It will activate the third sheet, “Sales 2017.”
Now, we will interchange the second and third sheets.
Technically, “Sales 2017” is our third sheet, and “Sales 2016 is our second sheet. So, now we will use the sheet index number as 3 and see what happens.
Code:
Sub Activate_Example1() Worksheets(3).Activate End Sub
In our view, it has to select the “Sales 2017” sheet, but it will select the “Sales 2016” sheet because in the order, “Sales 2016” is the third sheet.
So, activating the sheet by its name is always a safe option.
Example #2 – Activate Sheet by its Name
Now, we will see how to activate sheets by their name. In the place of a sheet index number, we need to mention the sheet name in double quotes.
Code:
Sub Activate_Example2() Worksheets("Sales 2016").Activate End Sub
When we run the code manually or using shortcut key F5, this would activate the sheet “Sales 2016” irrespective of the position in the workbook.
Not only the worksheets object, but we can also use the “Sheets” object to activate the sheet.
Below is the code.
Code:
Sub Activate_Example2() Sheets("Sales 2016").Activate End Sub
Worksheets can access only worksheets object and cannot access “Chart” sheets. However, if we use the Sheets object, we can access all the sheets in the workbook.
Example #3 – Activate Sheet from Another Workbook
Like how we need to mention the sheet name to activate the particular sheet, activating the sheet from another workbook also requires the “Workbook” name.
Code:
Sub Activate_Example3() Workbooks("Sales File.xlsx").Sheets("Sales 2016").Activate End Sub
It will activate the “Sales 2016” sheet from the workbook “Sales File.xlsx.”
Activate Sheet vs. Select Sheet Method
We can use methods to perform the same action, i.e., Activate and Select. However, there is a slight difference between these two methods.
#1 – Activate Method
By using the Activate method, we can only activate the specified worksheet.
For example, look at the below code.
Code:
Sub Activate_Example() Worksheets("Sales 2016").Activate End Sub
As we know, this code will select the worksheet “Sales 2016”.
#2 – Select Method
By using the Select method, we can perform other tasks as well.
Now, look at the below code.
Code:
This code not only activates the sheet “Sales 2016” but also selects the range of cells from A1 to A10.
Recommended Articles
This article has been a guide to VBA Activate Sheet. Here, we learn how to use the activate method in VBA to activate a particular Excel sheet, along with practical examples and downloadable templates. Below you can find some useful Excel VBA articles: –
- VBA TimeValue
- VBA DatePart Examples
- Option Explicit in VBA
- VBA Remove Duplicates
- VBA StatusBar
Return to VBA Code Examples
In this Article
- ActiveSheet
- Activate Worksheet (Setting the ActiveSheet)
- ActiveSheet Name
- Selected Sheets vs ActiveSheet
- Select Worksheet
- Select Worksheet by Tab Name
- Select Worksheet by Index Number
- Select Worksheet With VBA Code Name
- Select Current Worksheet
- More Activate / Select Sheet Examples
- Set ActiveSheet to Variable
- Change ActiveSheet Name
- With ActiveSheet
- Loop Through Selected Sheets
- GoTo Next Sheet
- VBA Coding Made Easy
This article will discuss the ActiveSheet object in VBA. It will also discuss how to activate, select, and go to Worksheets (& much more). Read our full VBA Worksheets Guide for more information about working with worksheets in VBA.
ActiveSheet
In VBA, ActiveSheet refers to the currently active Worksheet. Only one Sheet may be active at a time.
Activate Worksheet (Setting the ActiveSheet)
To set the ActiveSheet use Worksheet.Activate:
Worksheets("Input").Activate
The Activate Sheet command will actually “go to” the sheet, changing the visible Sheet.
The above example uses the Sheet (Tab) name. Instead you can use the VBA code name for the worksheet:
Sheet1.Activate
ActiveSheet Name
To get the ActiveSheet Name:
msgbox ActiveSheet.name
Selected Sheets vs ActiveSheet
At any point in time, only one Sheet can be the ActiveSheet. However, multiple Worksheets can be selected at once.
When multiple Worksheets are selected only the “top-most” Worksheet is considered active (the ActiveSheet).
Select Worksheet
If you would like to select a worksheet instead of activating it. Use .Select instead.
Select Worksheet by Tab Name
This selects a Worksheet based on it’s Sheet Tab Name
Sheets("Input").Select
Select Worksheet by Index Number
This selects a Worksheet based on it’s position relative to other tabs
Worksheets(1).Select
Select Worksheet With VBA Code Name
Sheet1.Select
Selecting worksheets by code name can prevent errors caused by worksheet name changes.
Select Current Worksheet
To select the current Worksheet, use the ActiveSheet object:
ActiveSheet.Select
More Activate / Select Sheet Examples
VBA Programming | Code Generator does work for you!
Set ActiveSheet to Variable
This will assign the ActiveSheet to a Worksheet Object Variable.
Dim ws As Worksheet
Set ws = ActiveSheet
Change ActiveSheet Name
This will change the ActiveSheet Name.
ActiveSheet.Name = "NewName"
With ActiveSheet
Using the With Statement allows you to streamline your code when working with objects (such as Sheets or ActiveSheet).
With ActiveSheet
.Name = "StartFresh"
.Cells.Clear
.Range("A1").Value = .Name
End With
Notice how you don’t need to repeat “ActiveSheet” before each line of code. This can be a huge time saver when working with a long list of commands.
Loop Through Selected Sheets
The following macro will Loop through all selected sheets, displaying their names.
Sub GetSelectedSheetsName()
Dim ws As Worksheet
For Each ws In ActiveWindow.SelectedSheets
MsgBox ws.Name
Next ws
End Sub
GoTo Next Sheet
This code will go to the next Sheet. If the ActiveSheet is the last Sheet, then it will go to the first Sheet in the Workbook.
If ActiveSheet.Index = Worksheets.Count Then
Worksheets(1).Activate
Else
ActiveSheet.Next.Activate
End If
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
<<Return to VBA Examples
VBA Activate Worksheet in Excel
VBA Activate Worksheet method is used to makes the current sheet as active sheet. Here we are the examples using Activate method of worksheet object in VBA. It is very frequently used method while writing VBA macros. We also see the VBA ActiveSheet object with examples.
- Why we need to Activate a Worksheet?
- VBA Activate Worksheet – Syntax
- VBA Activate Worksheet Method: Example 1
- VBA Activate Worksheet Method: Example 2
- VBA ActiveSheet Object
- Set ActiveSheet in Excel VBA
- VBA Activate Worksheet Method- Best Approach
- VBA Activate Worksheet Method- Instructions
When we need to use Activate Worksheet method in VBA?
We use Activate worksheet method to activate current sheet as active sheet. When we are working with multiple sheets in a workbook, if we want to move or go to another sheet in the same workbook or another workbook we use activate worksheet method.
VBA Activate Worksheet Method- Syntax
Here is the example syntax to activate Worksheet using VBA. You can use either a Worksheet name or Worksheet number. Always best practice is to use sheet name.
Worksheets(“Your Worksheet Name”).Activate
‘Or
Worksheets(“Worksheet Number”).Activate
Where Activate is the method of Workbook object is used to makes current sheet as active sheet.
VBA Activate Worksheet – with Name: Example 1
Please see the below VBA codes to activate Worksheet. In this example we are activating a Worksheet named “Project1”.
Sub Activate_Sheet() Worksheets("Project1").Activate 'Or Sheets("Project1").Activate End Sub
VBA Activate Worksheet Method– with Number: Example 2
Please see the below VBA code or macro procedure to activate Worksheet. In this example we are activating first Worksheet in the active workbook.
Sub Activate_Sheet_BasedOnIndex() Worksheets(1).Activate 'Or Sheets(1).Activate End Sub
VBA ActiveSheet Object
We can refer the currently activated worksheet using Excel VBA ActiveSheet object. ActiveSheet VBA object is very usefull while automating tasks and working on currently active sheet in the active workbook window. If you ignore the ActiveSheet object while refering any other object like range or chart, VBA will treat the ActiveSheet as the current sheet by default. For example: Following are the two macro statements both will refer the active sheet.
ActiveSheet.Range("A1")="Some Value" Range("A1")="Some Value"
both the above statements print some value at Range A1 of Activesheet.
Set ActiveSheet in Excel VBA
It is very convinient refer the Active Sheet in Excel VBA by setting into a Variable. We can assign and set ActiveSheet to an object and refer at any place of the procedure. Here is the syntax to Set ActiveSheet in VBA.
Sub sbSetAcitveSheetVBA() Dim ws As Worksheet Set ws = ActiveSheet ws.Range("A1") = "Some Value" End Sub
This code will print the some value at Range A1 of Activesheet.
VBA Activate Worksheet Method- Best Approach
Note: Always better to use the Worksheet name, instead of Worksheet number. The best is to assign the Worksheet to an object and then do whatever task you want to do with that particular Worksheet object.
When working with multiple Worksheets, you should refer the Worksheet with exact Worksheet name to correctly update your data into target Worksheet. Create Worksheet object and refer the Worksheet with the object whenever you require.
Let us see another example to understand the accessing the Worksheets using objects. You do not need to activate Worksheet to deal with any Worksheet.
Sub sb_Activate_Workbook_Object() 'Declare the objects here Dim wsMain As Worksheet, ws_A As Worksheet 'Set the worksheet to Object Set wsMain = ThisWorkbook Set ws_A = Worksheets("Test") 'Now deal with your worksheets ws_A.Range("A1") = wsMain.Sheet1.Range("A1") End Sub
VBA Activate Worksheet Method- Instructions
Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:
- Open an Excel Worksheet
- Press Alt+F11 :This will Open the VBA Editor. Otherwise, you can open it from the Developer Tab
- Insert a Module from Insert Menu
- Copy the above code for activating worksheet and Paste in the code window(VBA Editor)
- Save the file as macro enabled Worksheet
- Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line and check how the sheet is activating.
Related Resource
You have seen how to activate worksheets using Excel VBA and referring the AciveSheet. Following are the example macros to Activate Excel Workbook and Range.
- VBA to Activate Range
- VBA to Activate Workbook
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
Save Up to 85% LIMITED TIME OFFER
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates
Excel Pack
50+ Excel PM Templates
PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
-
-
- In this topic:
-
- When we need to use Activate Worksheet method in VBA?
- VBA Activate Worksheet Method- Syntax
- VBA Activate Worksheet – with Name: Example 1
- VBA Activate Worksheet Method– with Number: Example 2
- VBA ActiveSheet Object
- Set ActiveSheet in Excel VBA
- VBA Activate Worksheet Method- Best Approach
- VBA Activate Worksheet Method- Instructions
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
One Comment
-
jojo
February 14, 2019 at 7:15 PM — ReplyHello, in your last example you declare wsMain as WorkSheet and go on to set it as a Workbook and treat it as a Workbook, it this possible to do?
Thanks for your very helpful work.
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
Go to Top
Excel VBA Activate Sheet
For activating or selecting a sheet in excel, we manually go to the sheet and click on the Sheet tab. But when we write a code where we need to link the different sheets in different forms, then we can use the VBA Activate Sheet function. This can be done with the help of the “.Activate” command in VBA. This will automatically select or activate the sheet for further use.
Activating a sheet means selecting that sheet. And to know whether that sheet is activated or selected, it is better to check after running the code in excel if we are able to see the current sheet or not.
How to Activate the Sheet in Excel VBA?
Below are the different examples to activate sheet in excel using VBA code.
You can download this VBA Activate Sheet Excel Template here – VBA Activate Sheet Excel Template
VBA Activate Sheet – Example #1
There are various ways to activate a sheet in VBA. For this, we have considered 3 sheets named as First, Second and Third as shown below. Purpose of naming each sheet is to distinguish between sheet number and sheet name, which will be used in upcoming examples.
Follow the below steps to activate sheet in Excel VBA.
Step 1: Now go to VBA and from the Insert menu tab, select Module as shown below.
The module is the place where we will write the code for Activating Sheet. After that, we will get a new module opened.
Step 2: In that module, create Subcategory of function in any name. For better understanding, we have created it in the name of VBA Activate Sheet in sequence. This will help in tracking the proper module.
Code:
Sub VBA_ActivateSheet1() End Sub
Step 3: Now use Sheets function for selecting any sheet.
Code:
Sub VBA_ActivateSheet1() Sheets("First") End Sub
Step 4: Now to actually selecting or activating the sheet in excel use Activate command trailed by dot (.)
Code:
Sub VBA_ActivateSheet1() Sheets("First").Activate End Sub
By this, we allow the sheet named as “First” to get selected whenever the code calls for it.
Step 5: Now do the same for sheet name Second and Third as shown below.
Code:
Sub VBA_ActivateSheet1() Sheets("First").Activate Sheets("Second").Activate Sheets("Third").Activate End Sub
Step 6: Now compile the code step-by-step or in one go. The small code can be compiled in one go which may not give an error. After that run the code by clicking on the Play button which is below the menu bar.
We will see that the Third Sheet will get selected as it was located at the end. If we do step-by-step compile then we will be able to see who First, Second and Third Sheet are getting activated by pressing F8 Key.
As the compiler passes through the first line, we will see sheet First is activated as shown below.
Again pressing F8 Key, the compiler will pass through the second line of code and we can see sheet Second is activated.
And when compiler reached to End Sub of the code structure, we will sheet Third is activated as shown below.
VBA Activate Sheet – Example #2
We can also use Worksheet command to select and activate any sheet. This process is quite similar to example-1. For this again we will consider the file sheet name First, Second and Third. Follow the below steps to activate sheet in Excel VBA.
Step 1: In Module, start writing Subcategory of VBA Activate Sheet in sequence as shown below.
Code:
Sub VBA_ActivateSheet2() End Sub
Step 2: As we used Sheet command for activating Sheet, here we will use Worksheet command.
Code:
Sub VBA_ActivateSheet2() Worksheets("First") End Sub
Step 3: Now use Activate function trailed by dot(.) here as well to activate the sheet.
Code:
Sub VBA_ActivateSheet2() Worksheets("First").Activate End Sub
Step 4: Now do the same for Sheet Second and Third as well.
Code:
Sub VBA_ActivateSheet2() Worksheets("First").Activate Worksheets("Second").Activate Worksheets("Third").Activate End Sub
Step 5: Once done then compile the code. Here we will do step-by-step compile to see the changes. For press F8 which does step-by-step compiling and run the code as well. As the compiler passes through the second line of code, we will see in the Excel sheet Second is activated or selected.
Step 6: Again press the F8 key to select the next sheet. We will see, as compiler passes through the third line of code and reaches to end, sheet named Third is activated or selected.
VBA Activate Sheet – Example #3
There is another way to activate any worksheet. For this also we will consider the same set of sheets with same names First, Second and Third as used in the above examples. In this example, we will see 3 combinations of Sheet and Worksheet function along with Select, which we will use in place of Activate. The select function works in the same manner as Activate. Using a combination of different function for the same use we will see how the output varies.
Follow the below steps to activate sheet in Excel VBA.
Step 1: For this open a module and put the subcategory in the name of the performed function or in any other name as per your choice. We are keeping the name but in sequence, as shown below.
Code:
Sub VBA_ActivateSheet3() End Sub
Step 2: Again in the same manner use Sheets function with sheet name as shown below.
Code:
Sub VBA_ActivateSheet3() Sheets("First") End Sub
Step 3: After that use Select function trailed by dot (.)
Code:
Sub VBA_ActivateSheet3() Sheets("First").Select End Sub
Step 4: Now in the second line of code, we will use Sheets function along with Activate for sheet named Second as shown below.
Code:
Sub VBA_ActivateSheet3() Sheets("First").Select Sheets("Second").Activate End Sub
Step 5: And for the last sheet named as Third we will use Worksheets along with Select function to activate it as shown below.
Code:
Sub VBA_ActivateSheet3() Sheets("First").Select Sheets("Second").Activate Worksheets("Third").Select End Sub
Step 6: Now compile the complete code and run the code step-by-step to see the changes by pressing the F8 key. As the compiler passes through the first line of code, sheet First will be activated.
As the compile goes through the second line of code, sheet Second will be activated.
And at last, when compiler comes to End Sub, sheet Third will get activated as shown below.
This is how we can use a combination of Sheets and Worksheets along with Activate and Select functions to activate any worksheet.
Pros of Excel VBA Activate Sheet
- Selecting any sheet in excel as well as with VBA is quite easy.
- This function is quite helpful when our code is linked with data with different worksheets.
Things to Remember
- Select and Activate function has the same work of activating any worksheet. Although the name is different.
- Different combination of Select and Activate along with Sheets and Worksheet is possible for activating one or multiple worksheets.
- Activating any sheets means selecting of that worksheet.
- Once done with implementing code, save the file in Macro Enable Worksheet. This helps in saving the code so that we can use it multiple times without any loss.
Recommended Articles
This is a guide to VBA Activate Sheet. Here we discussed how to Activate sheet in Excel using VBA code along with some practical examples and downloadable excel template. You can also go through our other suggested articles –
- VBA Range Cells
- VBA RGB
- VBA Name Worksheet
- VBA Delete Sheet