Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Select Worksheet by Name (Step-by-Step)

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To select a worksheet by name in Excel VBA, use the Worksheets collection with the sheet name in quotes, for example Worksheets("Summary").Select. Before that line runs, the sheet must exist in the active workbook and that workbook must be active, otherwise VBA raises a runtime error. If you only need to work with the sheet rather than visibly select it, assign it to an object variable with Set ws = Worksheets("Summary") and act on ws; selecting is only necessary when you want the sheet brought to the front for the user.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Menu labels can differ slightly by version and region.
  • A workbook that contains at least one named worksheet. The name must match exactly, including spaces and capitalisation, though VBA's default comparison is case-insensitive.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module.
  • Macro security set to allow macros in the workbook you are testing (File > Options > Trust Center on Windows; Excel > Preferences > Security on Mac).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. The editor opens in a separate window.
  2. Insert a standard module. In the editor, choose Insert > Module. A blank code pane appears. Keeping code in a standard module makes it easy to run with Run > Run Sub or F5.
  3. Write the basic selection line. Type a short procedure such as:
    Sub SelectSheet()
    Worksheets("Summary").Select
    End Sub
    Replace Summary with the exact tab name you see at the bottom of the Excel window.
  4. Make sure the right workbook is active. Worksheets without a workbook qualifier refers to the active workbook. If your macro runs from a different file, qualify it: Workbooks("Budget.xlsx").Worksheets("Summary").Select. The workbook must be open; otherwise you get a subscript out of range error.
  5. Run the macro. Place the cursor inside the procedure and press F5, or use Run > Run Sub/UserForm. Excel switches to the named sheet.
  6. Handle a missing sheet safely. Wrap the call so a renamed or deleted tab does not crash the macro:
    On Error Resume Next
    Worksheets("Summary").Select
    If Err.Number <> 0 Then MsgBox "Sheet not found"
    On Error GoTo 0
    This reports the problem instead of stopping abruptly.
  7. Prefer an object variable when you do not need to show the sheet. Use Dim ws As Worksheet then Set ws = Worksheets("Summary") and refer to ws.Range("A1"). This avoids the flicker and focus changes that Select causes and is faster in loops.
  8. Activate versus select. Activate makes the sheet the active one; Select works on the sheet object and is the common choice for a single sheet. Both require the workbook to be active. If you select multiple sheets, the first must be activated before others can be selected.
  9. Use the sheet's code name if the tab may be renamed. In the Properties window (F4) you can see a name like Sheet1. Referring to Sheet1.Select keeps working even if the visible tab name changes, which is useful for templates.
  10. Test edge cases. Try a name with a space, a name with an apostrophe, and a name that does not exist. Names with apostrophes need the apostrophe doubled inside the string, for example Worksheets("Team's Data") works as written because the apostrophe is inside double quotes; only VBA string literals need care with double quotes, which are escaped by doubling them.

Common Mistakes

  • Using the workbook's file name instead of the worksheet tab name.
  • Forgetting that Worksheets targets the active workbook, so the macro selects a sheet in the wrong file.
  • Leaving a trailing space in the quoted name, which causes a subscript out of range error.
  • Assuming Select works when the workbook is not active; activate the workbook first or qualify the reference.
  • Using Sheets and Worksheets interchangeably. Sheets also includes chart sheets, so a chart named the same as a worksheet can cause confusion.
  • Relying on the tab name in a template that users rename; use the code name instead.

Tips

  • Avoid Select in loops. Set a worksheet variable once and read or write through it; this is noticeably faster on large workbooks.
  • Use ThisWorkbook.Worksheets("Summary") when the macro lives in the same file, so it always points at the right workbook regardless of what is active.
  • Build the sheet name from a variable so the same routine can handle several tabs: Worksheets(sheetName).Activate.
  • Add a small helper function that checks whether a sheet exists before selecting it, using On Error Resume Next and testing the object.
  • Turn off ScreenUpdating around heavy code, then restore it, to reduce flicker.

Troubleshooting

If you get Subscript out of range, the name does not match a sheet in the active workbook. Check spelling, spaces and whether the workbook is actually open. If you get Select method of Worksheet class failed, the workbook is not active or the sheet is hidden; unhide it or activate the workbook first. If the macro runs but nothing appears to happen, the sheet may already be active or the code may be in a module that is not being run; confirm with a message box. If the macro stops on a protected workbook, unprotect it or ask the owner, since protection blocks selection changes. If errors persist, save and reopen the workbook, then re-run the procedure from a fresh module.

Frequently Asked Questions

Can I select a sheet by its tab name if the name contains spaces?

Yes. Put the full name in quotes, for example Worksheets("Monthly Summary").Select.

What is the difference between Sheets and Worksheets?

Worksheets refers only to worksheet tabs; Sheets includes chart sheets and other sheet types as well.

Why does my macro select a sheet in the wrong workbook?

Because Worksheets uses the active workbook. Qualify the reference with ThisWorkbook or Workbooks("name.xlsx").

Can I select a sheet that is hidden?

Not while it is hidden. Set its Visible property to xlSheetVisible first, then select it.

Does selecting a sheet slow down my macro?

Yes, compared with working through a worksheet variable. Use Set ws = Worksheets("Summary") for speed.

What happens if the sheet name changes later?

Code that uses the tab name breaks. Use the sheet's code name, such as Sheet1, if the tab may be renamed.