HOWTO · VBA

How to Activate Worksheet in Excel in VBA

This article helps you understand how to activate a worksheet in Excel using VBA. Learn various methods, including activating by name, index, and variables. With practical examples and clear explanations, this guide will enhance your Excel skills and streamline your workflows.

On this page

To make a worksheet current in Excel VBA, call its Activate method. For example, Worksheets("Sheet2").Activate selects the worksheet named Sheet2. When code only needs to read or change cells, it is often clearer to refer to the worksheet directly without changing the active sheet.

Activate a Worksheet by Name

Use the worksheet tab name in the Worksheets collection. A minimal VBA procedure is Sub ShowSummary(): Worksheets("Summary").Activate: End Sub. Replace Summary with the exact tab name, including spaces. The name in this collection is the visible worksheet name, not the sheet’s VBA code name. The member name is Activate, and the object type used for a sheet reference is Worksheet.

This small Python model demonstrates looking up a worksheet by its tab name and reading a cell value:

worksheets = {
    "Summary": {"A1": "Ready"},
    "Archive": {"A1": "Saved"},
}
selected = worksheets["Summary"]
print(selected["A1"])
Ready

The model illustrates name-based selection; in Excel VBA, use the Worksheets("Summary").Activate statement above. The workbook must contain a worksheet with that exact tab name.

Qualify the collection with a workbook when the target may not belong to the workbook containing the macro: Workbooks("Reports.xlsx").Worksheets("Summary").Activate. The workbook must be open, and its name must match the open workbook. This is clearer than relying on whichever workbook happens to be active.

Activate a Worksheet by Index

A worksheet can also be selected by its position: Worksheets(2).Activate. The index is one-based, so Worksheets(1) refers to the first worksheet in that workbook’s Worksheets collection. Indexes are useful when processing sheets by order, but they can point to a different worksheet after sheets are inserted, deleted, or moved. Use a name when the sheet’s identity matters more than its position.

Refer to the Active Sheet with ActiveSheet

ActiveSheet refers to the sheet currently selected in Excel. For example, ActiveSheet.Range("A1").Value = "Ready" writes to cell A1 of that sheet. Unqualified Range("A1") also uses the active worksheet in a worksheet context, so it depends on Excel’s current selection. That dependency can make a procedure affect the wrong sheet if another action changes the active sheet. Prefer an explicit worksheet reference when the target is known.

Activating a sheet changes Excel’s visible selection; it is not required merely to edit that sheet. A direct reference such as Worksheets("Summary").Range("A1").Value = "Ready" names the target explicitly.

Use a Worksheet Variable

A Worksheet variable keeps a reference to a particular worksheet so later statements can reuse it. Declare the variable, assign the object with Set, then call Activate or work with its cells. For example, the procedure’s statements are Dim ws As Worksheet, Set ws = ThisWorkbook.Worksheets("Summary"), and ws.Activate. ThisWorkbook means the workbook containing the VBA code; use a Workbooks("Reports.xlsx") reference when the target is a different open workbook.

After assigning ws, a cell operation can use ws.Range("A1").Value = "Ready" without activating the sheet. This keeps the destination explicit even if the user changes the selected sheet.

Activate Worksheets in a Loop

When the task truly requires each worksheet to become visible in turn, iterate over the Worksheets collection. A VBA loop can be written as Dim ws As Worksheet, followed by For Each ws In ThisWorkbook.Worksheets, ws.Activate, and Next ws. Put the action that needs the active sheet inside the loop. Worksheets excludes chart sheets; Sheets includes both worksheets and chart sheets, so use the former for worksheet-only work.

For most repeated cell operations, activation is unnecessary: put a direct operation such as ws.Range("A1").Value = "Ready" inside the loop instead. This avoids relying on the active selection and makes each target clear.

Handle Common Activation Errors and Limits

The name or index must identify a worksheet in the specified workbook. A misspelled or missing name, a closed workbook, or an out-of-range index causes VBA to report a runtime error. Check the workbook and tab names, and confirm that the target exists before activating it.

A hidden worksheet cannot be activated while it remains hidden. Make it visible first, for example with Worksheets("Summary").Visible = xlSheetVisible, then activate it if the user needs to see it. A very hidden worksheet must also have its visibility changed from VBA or the Visual Basic Editor before activation.

Excel has one active sheet at a time in the active workbook. A workbook can contain many worksheets, but activating another one replaces the current active sheet. If the goal is to update several sheets, loop through worksheet references and act on each directly rather than trying to keep multiple sheets active.