To activate a worksheet in VBA, use the Activate method on a Worksheet object, for example Worksheets("Sheet2").Activate or Sheets("Summary").Activate. The sheet must be visible and the workbook must be the active workbook (or you must qualify the reference with the workbook). That single line is the whole answer; the rest of this guide covers the correct syntax, the difference between activating and selecting, and the errors you are most likely to hit.
What You'll Need
- Excel with macros enabled (the Developer tab visible, or the ability to open the VBA editor with Alt+F11).
- A workbook that contains at least one worksheet you want to activate.
- Basic familiarity with the VBA editor: inserting a module and running a macro.
- Macro security set to allow the file to run (Trust Center settings), or the workbook saved as .xlsm.
Step-by-Step
- Open the VBA editor. In Excel, press Alt+F11 (on Mac, Fn+Alt+F11 depending on keyboard settings). The Visual Basic Editor opens in a separate window.
- Insert a standard module. In the editor menu, choose Insert > Module. A blank code window appears. (Menu labels can differ slightly by Excel version and platform, but Insert > Module is standard.)
- Write the activation line. Type a short procedure such as:
Sub ActivateSheet()
Worksheets("Sheet2").Activate
End Sub
ReplaceSheet2with the exact name shown on the sheet tab. If the name contains spaces, keep the quotation marks:Worksheets("Sales 2024").Activate. - Run the macro. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. Excel switches to the named sheet if it is visible.
- Use the sheet's code name if the tab name may change. Every sheet has a code name (visible in the Properties window, usually Sheet1, Sheet2 and so on).
Sheet2.Activateworks without quotation marks and survives tab renames, but it is less readable. Pick one approach and stay consistent. - Qualify the workbook when activating from another workbook. If your macro lives in a different file,
Worksheets("Sheet2").Activatemay target the wrong workbook. UseWorkbooks("Book1.xlsx").Worksheets("Sheet2").Activate, or activate the workbook first withWorkbooks("Book1.xlsx").Activate. - Activate the sheet before writing to it. A common pattern is to activate a sheet, then write values or select a range. For example:
Worksheets("Report").Activate
Range("A1").Value = "Done"
Note that you do not strictly need to activate a sheet to write to it; you can write directly withWorksheets("Report").Range("A1").Value = "Done". Activation is mainly for the user's view. - Save and test. Save the workbook as .xlsm if it is not already macro-enabled, then run the macro again to confirm it behaves as expected.
Common Mistakes
- Using the wrong sheet name.
Worksheets("Sheet 2")fails if the tab is actuallySheet2(no space). Names are case-insensitive but spacing and punctuation must match. - Activating a hidden sheet. The
Activatemethod fails on a sheet whoseVisibleproperty isxlSheetHiddenorxlSheetVeryHidden. SetWorksheets("Sheet2").Visible = xlSheetVisiblefirst, or useWorksheets("Sheet2").Selectonly after making it visible. - Confusing Activate with Select.
Activatemakes a sheet the active sheet.Selectselects the sheet object; on a worksheet, both usually produce the same visible result, butSelectcan fail if the workbook is not active. UseActivatefor switching sheets. - Forgetting to qualify the workbook. Running the macro while a different workbook is active can activate a sheet in the wrong file or raise a subscript out of range error. Always qualify when more than one workbook is open.
- Using
Sheetswhen you meanWorksheets.Sheetsincludes chart sheets, soSheets("Chart1").Activateis valid butWorksheets("Chart1")is not. UseWorksheetsfor worksheets only. - Assuming activation is required. Many macros do not need to activate a sheet at all; direct references are faster and avoid screen flicker. Activate only when the user needs to see the sheet.
Tips
- Use
Application.ScreenUpdating = Falseat the start andTrueat the end when your macro activates several sheets in a row; this reduces flicker and speeds up execution. - Store sheet names in constants or variables so a rename only needs one edit:
Const ReportSheet As String = "Report". - If you want to activate a sheet and a specific cell, do it in two lines:
Worksheets("Report").ActivatethenRange("B2").Select. - Prefer
ThisWorkbook.Worksheets("Report").Activatewhen the macro is stored in the same workbook;ThisWorkbookalways refers to the file containing the code. - For a user-facing macro, add error handling so a renamed or deleted sheet gives a clear message rather than a runtime error.
Troubleshooting
If the macro does not switch sheets, work through these checks in order.
- Check the sheet name exactly. Look at the tab and compare character by character, including spaces. A trailing space is easy to miss.
- Check visibility. Right-click the sheet tab and see whether Unhide is available. If it is, the sheet is hidden and must be made visible before activation.
- Check which workbook is active. If several workbooks are open, qualify the reference with
Workbooks("name.xlsx")or useThisWorkbook. - Check for protected structure. If the workbook structure is protected, some sheet operations are blocked. Unprotect the workbook structure (Review > Protect Workbook) before running the macro, then re-protect it afterwards.
- Check the error number. Run-time error 9 (Subscript out of range) usually means the sheet name does not exist in the referenced workbook. Run-time error 1004 often means the sheet is hidden or the workbook is not active.
- Step through the code. Press F8 to run one line at a time and watch which line fails; this isolates whether the problem is the sheet reference or something later in the macro.
Frequently Asked Questions
What is the difference between Activate and Select in VBA?
Activate makes an object the active one, such as the active sheet. Select adds an object to the current selection. For worksheets, both usually bring the sheet to the front, but Activate is the clearer choice for switching sheets.
Can I activate a sheet without making it visible?
No. The Activate method fails on hidden and very hidden sheets. Set Visible = xlSheetVisible first if the user needs to see it.
Do I need to activate a sheet before writing to it?
No. You can write directly with a qualified reference such as Worksheets("Report").Range("A1").Value = 1. Activation is only needed when the user should see that sheet.
Why do I get run-time error 9 when activating a sheet?
Error 9 means the name you supplied does not exist in the workbook being referenced. Check spelling, spacing and which workbook is active.
How do I activate a sheet in a different workbook?
Qualify the reference: Workbooks("Book1.xlsx").Worksheets("Sheet2").Activate. The target workbook does not need to be active for this to work, but it must be open.
Can I activate a sheet when the workbook is protected?
If only the worksheet is protected, activation still works. If the workbook structure is protected, unprotect it first, because structural protection blocks some sheet operations.