Excel VBA: Reference a Worksheet by Name
In this article
To reference a worksheet by name in Excel VBA, use Worksheets("Sheet1") or Sheets("Sheet1") and then chain the property or method you need — for example Worksheets("Sales").Range("A1").Value = 100. If the name might change or you want the code to survive a rename, use the worksheet's code name instead, such as Sheet1.Range("A1"), or point at it with ThisWorkbook.Worksheets("Sales"). The name you pass must match the tab name exactly, including spaces and capitalisation.
Referencing by name is the most readable way to write VBA. Anyone opening the code can see which sheet is being touched, and the code keeps working when someone reorders the tabs. The catch is that a name reference fails with a runtime error the moment the tab is renamed or missing, so the sections below also cover how to check for the sheet first and how to avoid the usual mistakes.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels can differ slightly by version and platform.
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code.
- The exact tab names of the sheets you intend to reference. Turn on the Immediate window in the VBA editor (Ctrl+G) so you can test names quickly.
- Basic familiarity with opening the VBA editor with Alt+F11 (Windows) or Option+F11 (Mac).
Step-by-Step
- Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. In the Project Explorer, find your workbook and insert a module with Insert > Module so the code lives somewhere reusable.
- Start a sub procedure. Type
Sub ReferenceByName()and press Enter. The editor addsEnd Subautomatically. - Write the simplest name reference. Add the line
Worksheets("Sales").Range("A1").Value = "Hello". This writes to cell A1 on the tab named Sales. Note the quotation marks — without them VBA treats the word as a variable and throws an error. - Qualify it with the workbook. Change the line to
ThisWorkbook.Worksheets("Sales").Range("A1").Value = "Hello". ThisWorkbook always means the workbook containing the code, so the line keeps working even when another workbook is active. UseActiveWorkbookonly when you deliberately want whatever file is in front. - Read a value instead of writing one. Swap the assignment for
MsgBox ThisWorkbook.Worksheets("Sales").Range("A1").Valueto confirm you are pointing at the right sheet. - Store the reference in a variable. Declare
Dim ws As Worksheet, thenSet ws = ThisWorkbook.Worksheets("Sales"). From then on usews.Range("A1"),ws.Cells(2, 1)and so on. This is faster and easier to read than repeating the long reference. - Guard against a missing sheet. Wrap the assignment in error handling, or loop through the Worksheets collection checking
If ws.Name = "Sales" Then. A simple pattern isOn Error Resume Next, theSetline, thenIf ws Is Nothing Then MsgBox "Sheet not found", followed byOn Error GoTo 0to restore normal error behaviour. - Decide between Worksheets and Sheets.
Worksheetscovers ordinary grid sheets only.Sheetsalso covers chart sheets, so useSheets("Chart1")when the target might be a chart. For plain data tabs, Worksheets is the safer, more explicit choice. - Consider the code name for rename-proof code. Every sheet has a code name visible in the Properties window of the VBA editor (press F4). If the Sales tab shows
Sheet1there,Sheet1.Range("A1").Value = "Hello"keeps working after the tab is renamed, because the code name does not change. The trade-off is that the code is less readable. - Run and test. Press F5 with the cursor inside the sub. If you get a subscript out of range error, the name does not match a tab — check spelling, trailing spaces and whether the sheet is in the workbook you qualified.
Common Mistakes
- Missing quotation marks.
Worksheets(Sales)looks up a variable called Sales and fails. It must beWorksheets("Sales"). - Wrong workbook. An unqualified
Worksheets("Sales")refers to the active workbook. If the user clicks another file, the code targets the wrong place or errors. Qualify with ThisWorkbook. - Name mismatch. Tab names are case-insensitive for lookup but must otherwise match exactly, including spaces. "Sales " with a trailing space is a different name.
- Confusing the tab name with the code name. The name in the Properties window (Sheet1) is not the tab name (Sales). Mixing them up causes subscript out of range errors.
- Using Sheets when a chart sheet shares the name. If a chart sheet and a worksheet could both match, be explicit about which collection you mean.
- Hard-coding a name that users will rename. If the workbook is maintained by others, a code-name reference or a name stored in a cell is more robust.
Tips
- Use a
Withblock for several operations on one sheet:With ThisWorkbook.Worksheets("Sales")…End With, then refer to.Rangeand.Cellsinside it. - Keep sheet names in a constant at the top of the module so a rename means editing one line:
Const SalesSheet As String = "Sales". - Use the Immediate window to test a name before writing the full routine: type
?Worksheets("Sales").Nameand press Enter. - Prefer
ws.Cells(row, column)over building address strings when looping, since it avoids string concatenation and is easier to read. - If the workbook will be shared, add a short comment above the sub listing the tab names it depends on.
Troubleshooting
Runtime error 9, subscript out of range. The name does not exist in the workbook you qualified. Check the tab name character by character, confirm you used ThisWorkbook rather than ActiveWorkbook, and confirm the sheet has not been deleted or hidden in a way that matters to your code.
Runtime error 438, object doesn't support this property or method. You are probably calling a worksheet method on something that is not a worksheet, often because a chart sheet matched the name. Switch to Worksheets or check the object type.
The code writes to the wrong sheet. Remove the unqualified reference and add ThisWorkbook. Test by displaying ws.Name in a message box before writing.
It worked, then stopped after a rename. Expected with name references. Either update the string, switch to the code name, or add the guard from step 7 so the code reports the problem instead of failing silently.
Macros are blocked. Check the Trust Center macro settings and confirm the file is saved as .xlsm. If the workbook came from elsewhere, you may need to unblock it in file properties on Windows.
Frequently Asked Questions
What is the difference between Worksheets("Name") and Sheets("Name")?
Worksheets only returns worksheet objects. Sheets returns worksheets and chart sheets, so it is broader. Use Worksheets unless you specifically need chart sheets.
Can I reference a sheet by its index number instead?
Yes: Worksheets(1) refers to the first tab. It is fragile because inserting or moving tabs changes the order, so name references are usually better.
How do I reference a sheet in a different open workbook?
Qualify it with the workbook object, for example Workbooks("Budget.xlsx").Worksheets("Sales"). The workbook must be open, or you need to open it first with Workbooks.Open.
Why does my code fail after I rename a tab?
Name references are literal strings. Renaming the tab breaks the match. Use the sheet's code name or update the string.
Can I reference a sheet whose name contains a space?
Yes. Put the whole name inside the quotes: Worksheets("Monthly Sales"). Spaces are fine; only the quotes matter.
How do I check whether a sheet exists before using it?
Loop through the Worksheets collection comparing each .Name, or use On Error Resume Next around the Set line and test whether the variable Is Nothing afterwards.