Vba как активировать лист excel

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

VBA Activate Sheet

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkArticle Link to be Hyperlinked
For eg:
Source: VBA Activate Sheet (wallstreetmojo.com)

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

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.

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

Make a particular worksheet visible using a macro in Excel.

This is called activating a worksheet and is rather easy to do.

Sections:

Activate Worksheet by Name

Activate Worksheet by Index Number

Notes

Activate Worksheet by Name

Let’s navigate to, activate, the worksheet called «Sheet2».

Worksheets("Sheet2").Activate

08008f3433d614b4eb713fdcde4e01eb.jpg

Worksheets(«Shee2») is how we reference the sheet to which we want to navigate.

Sheet2 is the name of the sheet to which we want to navigate. Make sure to surround it with double quotation marks.

Activate is what actually takes the user to that worksheet.

Putting it all together, we get: Worksheets(«Sheet2»).Activate

It’s as simple as that.

Activate Worksheet by Index Number

This time, let’s navigate to the second worksheet using its index number.

All we do is to replace «Sheet2» from the last example with 2.

Worksheets(2).Activate

3e8969b16e6f131afc29445b0f9a02ac.jpg

2 is the index number of the desired worksheet and Activate is what takes us to the desired worksheet.

This example can be confusing if you are not used to using index numbers, but index numbers are really helpful when you have to do something like loop through all of the worksheets in the workbook.

Notes

Basically, just reference the desired worksheet, however you want, and then type .Activate after it.

You should not use the Activate feature to navigate to a worksheet so that you can get data from it or put data into it. This is very bad Macro/VBA design and it will make your life hell later on. To get data from separate worksheets, read this tutorial: Select Data from Separate Worksheets with Macros VBA in Excel.

This feature should be used when you want the user to end up on a specific worksheet.

Make sure to download the sample file attached to this tutorial so that you can see this macro in Excel and work with it.

Similar Content on TeachExcel

Copy and Paste Data using Macro VBA in Excel

Tutorial: How to copy and paste data using a Macro in Excel. I’ll show you multiple ways to do this,…

Select Data from Separate Worksheets with Macros VBA in Excel

Tutorial: Select data from other worksheets with Macros and VBA without navigating to those workshee…

Get the Last Row using VBA in Excel

Tutorial:
(file used in the video above)
How to find the last row of data using a Macro/VBA in Exce…

Login to a Website using a Macro

: Connect and login to a website using a macro in Excel.
This allows you to open a website a…

Get the Name of a Worksheet in Macros VBA in Excel

Tutorial: How to get the name of a worksheet in Excel using VBA and Macros and also how to store tha…

Guide to Creating Charts with a Macro in Excel

Tutorial: How to add, edit, and position charts in Excel using VBA.
This tutorial covers what to do …

Subscribe for Weekly Tutorials

BONUS: subscribe now to download our Top Tutorials Ebook!

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

  1. VBA to Create New Sheet
  2. How to Record a Macro in Excel
  3. 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).

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.

vba activate sheet

The above example uses the Sheet (Tab) name.  Instead you can use the VBA code name for the worksheet:

Sheet1.Activate

vba activesheet

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

vba selected sheets

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

vba select sheet

Select Worksheet by Index Number

This selects a Worksheet based on it’s position relative to other tabs

Worksheets(1).Select

vba select sheet index number

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!

Select Activate Worksheet

Learn More!

<<Return to VBA Examples

Skip to content

VBA Activate Worksheet in Excel

  • VBA Activate Method Excel Worksheet Object

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.

VBA Activate Method Excel Worksheet Object

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

  1. Open an Excel Worksheet
  2. Press Alt+F11 :This will Open the VBA Editor. Otherwise, you can open it from the Developer Tab
  3. Insert a Module from Insert Menu
  4. Copy the above code for activating worksheet and Paste in the code window(VBA Editor)
  5. Save the file as macro enabled Worksheet
  6. 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
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

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
Excel VBA Project Management Templates
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:
By PNRaoLast Updated: March 2, 2023

One Comment

  1. jojo
    February 14, 2019 at 7:15 PM — Reply

    Hello, 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

With Our Professional and Premium Project Management Templates!

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.

Analysistabs Logo

Page load link

VBA Projects With Source Code

3 Realtime VBA Projects
with Source Code!

Take Your Projects To The Next Level By Exploring Our Professional Projects

Go to Top

Понравилась статья? Поделить с друзьями:
  • Vba имя списка excel
  • Vba изменить рисунок excel
  • Vba изменение значений ячеек в excel в одну
  • Vba значение поля word
  • Vba запрет на редактирование ячейки в excel