VBA Activate Sheet

Last Updated :

-

Edited by :

Reviewed by :

Table Of Contents

arrow

Excel VBA Activate Sheet

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 keys, 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.

VBA Activate Sheet

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.

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

VBA Activate sheet Example 1

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
VBA Activate sheet Example 1-1

When you run the code using the F5 key or manually, this will activate the second sheet, “Sales 2016”.

vba activate sheet Example 1-2.mp4

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
VBA Activate sheet Example 1-2

It will activate the third sheet, “Sales 2017.”

VBA Activate sheet Example 1-3.png

Now, we will interchange the second and third sheets.

VBA Activate sheet Example 1-4

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
VBA Activate sheet Example 1-2

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.

vba activate sheet Example 1-4

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
VBA Activatesheet Example 2

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.

vba activatesheet Example 2-2

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
VBA Activatesheet Example 2-1

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
Activatesheet Example 2-2

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
VBA Activate sheet vs select sheet

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:

Activate sheet vs select sheet 1

This code not only activates the sheet “Sales 2016” but also selects the range of cells from A1 to A10.