Excel VBA: How to Select a Worksheet
In this article
To select a worksheet in Excel VBA, use the Worksheets or Sheets collection and call its Select method, for example Worksheets("Sheet2").Select. The sheet must be visible and the workbook must be active, otherwise the code raises an error. This guide shows the exact syntax, how to avoid the most common runtime errors, and how to activate a sheet without selecting it.
What You'll Need
- Excel with macros enabled (the Developer tab is not required for typing code, but it helps).
- A workbook that contains at least two worksheets, so you can see the selection change.
- Basic familiarity with opening the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in a separate window. Menu labels can differ slightly by Excel version and platform; the code itself is the same.
- Insert a module. In the editor, choose Insert > Module. A blank code pane appears. You can also use a sheet module, but a standard module keeps the example reusable.
- Write the simplest selection statement. Type the following into the module:
This selects the worksheet named Sheet2. Replace the name with your own sheet name, exactly as it appears on the tab.Sub SelectSheet() Worksheets("Sheet2").Select End Sub - Run the macro. Place the cursor inside the subroutine and press F5, or choose Run > Run Sub/UserForm. Excel switches to the named sheet. If the sheet is hidden, you get run-time error 1004; see Troubleshooting.
- Select by index number instead of name. If you prefer not to hard-code a name, use the sheet's position in the workbook:
This selects the second sheet from the left, counting only worksheets (not chart sheets).Worksheets(2).Select - Select the active sheet. To refer to whatever sheet is currently active, use
ActiveSheet.Select. This is rarely needed because the active sheet is already selected, but it is useful when you have changedActiveSheetthrough other code. - Select a sheet in another workbook. Qualify the collection with the workbook object:
The target workbook must be open. If it is not, the code fails; open it first withWorkbooks("Book2.xlsx").Worksheets("Data").SelectWorkbooks.Open. - Select multiple sheets at once. Pass an array of sheet names to
Sheets:
All named sheets must be visible. Selecting a group is useful before formatting or printing several sheets together.Sheets(Array("Sheet1", "Sheet3")).Select - Use Activate when you only need the sheet active.
Worksheets("Sheet2").Activatemakes the sheet active without changing the selection of cells or shapes. For most automation,Activateis safer thanSelectbecause it does not depend on the current selection state. - Save and test. Save the workbook as an .xlsm file so the macro is retained. Run the macro again after switching to a different sheet to confirm it behaves as expected.
Common Mistakes
- Using the tab caption instead of the sheet name. The name in
Worksheets("...")is the sheet's Name property, not the text shown on the tab if the tab has been renamed. Check the name in the Properties window or the Name Box. - Selecting a hidden sheet.
Selectfails on hidden sheets. SetVisibletoxlSheetVisiblefirst, or useActivateafter unhiding. - Forgetting to qualify the workbook. If more than one workbook is open, an unqualified
Worksheets("Sheet2")refers to the active workbook, which may not be the one you expect. - Assuming
SheetsandWorksheetsare the same.Sheetsincludes chart sheets and other sheet types;Worksheetsincludes only worksheets. Use the collection that matches the sheet type you are targeting. - Relying on
Selectinside loops. Selecting a sheet on every iteration is slow and can trigger screen flicker. Work with the sheet object directly instead.
Tips
- Turn off screen updating while a macro selects sheets repeatedly:
Application.ScreenUpdating = Falseat the start, then set it back toTruebefore the macro ends. - Use a variable to hold the sheet reference:
Dim ws As Worksheet: Set ws = Worksheets("Sheet2"). Then callws.Selectorws.Activate. This makes the code easier to read and change. - Prefer
ActivateoverSelectwhen you only need the sheet in view.Selectchanges the selection of cells and objects, which can interfere with later steps. - If the sheet name might contain spaces or special characters, keep the quotation marks around it; VBA handles the rest.
- Use
ThisWorkbook.Worksheets("Sheet2")when the macro lives in the same workbook as the target sheet. This avoids ambiguity if the user switches to another workbook while the macro runs.
Troubleshooting
If the macro does not select the sheet you expect, work through these checks in order.
- Run-time error 9 (Subscript out of range). The sheet name does not exist in the workbook being referenced. Check spelling, including spaces, and confirm the workbook is the one you think it is.
- Run-time error 1004. Common causes are a hidden sheet, a protected workbook, or a sheet name that does not exist. Unhide the sheet (right-click a tab > Unhide) or unprotect the workbook, then run again.
- Nothing happens. The macro may have run on a different workbook, or screen updating may be off from a previous run. Press Ctrl+Break to stop execution, then check
Application.ScreenUpdatingand the workbook reference. - The wrong sheet is selected. If you used an index number, remember that the index counts only worksheets and follows tab order. Inserting or moving sheets changes the index.
- The macro is disabled. Check the Trust Center settings in Excel and confirm macros are enabled for the file location. If the file came from another source, you may need to unblock it in file properties.
Frequently Asked Questions
What is the difference between Select and Activate in VBA?
Select makes the sheet the current selection, which can include multiple sheets. Activate makes a single sheet active without changing the selection of cells or objects. For most automation, Activate is the safer choice.
Can I select a sheet without knowing its name?
Yes. Use its index number, such as Worksheets(1), or loop through the Worksheets collection and test a property such as Visible or Name before selecting.
Why does selecting a hidden sheet fail?
Excel does not allow a hidden sheet to be selected. Set Worksheets("Sheet2").Visible = xlSheetVisible first, then select it. Remember that unhiding changes the workbook, so save only if that is intended.
How do I select the last worksheet in the workbook?
Use Worksheets(Worksheets.Count).Select. The Count property returns the number of worksheets, so the last one is at that index.
Does selecting a sheet work on Excel for Mac?
Yes, the VBA object model is the same. The keyboard shortcut to open the editor is Option+F11 instead of Alt+F11, and some dialog labels differ, but Worksheets("Sheet2").Select behaves the same way.
Can I select a sheet in a closed workbook?
No. The workbook must be open before you can select a sheet in it. Open it first with Workbooks.Open, then select the sheet, and close it when finished if needed.