Excel VBA Worksheet Data Type: How to Use It
In this article
Use the Worksheet data type in VBA to hold a reference to a specific sheet, so you can read and write its cells, ranges, and properties without repeatedly looking the sheet up by name. Declare a variable As Worksheet, then assign it with Set ws = ThisWorkbook.Worksheets("Sheet1"). That is the whole idea: a Worksheet variable is an object reference, and you must use Set to assign it. The steps below show the exact syntax, how to loop through every sheet safely, and the errors that trip people up.
What You'll Need
- Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer). Menu labels can differ slightly by version and region.
- A workbook with at least two worksheets, so you can test looping and referencing by name.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows; on Mac, Tools > Macro > Visual Basic Editor).
- Macro security set to allow macros in your test file. If macros are disabled, code will not run.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or use Tools > Macro > Visual Basic Editor (Mac). In the Project Explorer, find your workbook and insert a new module with Insert > Module.
- Declare a Worksheet variable. At the top of a new subroutine, type
Dim ws As Worksheet. TheAs Worksheetpart is the data type: it tells VBA the variable will point to a worksheet object, not to text or a number. - Assign a sheet to the variable using Set. Add
Set ws = ThisWorkbook.Worksheets("Sheet1"). ReplaceSheet1with the tab name exactly as shown, including spaces. If the name is wrong, you get run-time error 9 (Subscript out of range). - Use the variable to read or write cells. For example,
ws.Range("A1").Value = "Hello"writes to A1 on that sheet, andMsgBox ws.Range("A1").Valuereads it back. Becausewsalready points to the sheet, you do not need to name the sheet again. - Loop through all worksheets. Declare
Dim ws As Worksheetand then writeFor Each ws In ThisWorkbook.Worksheets...Next ws. Inside the loop,ws.Namegives the tab name andws.Range("A1").Valuereads A1 on each sheet in turn. This avoids hard-coding sheet names. - Reference a sheet by index when names change. Use
Set ws = ThisWorkbook.Worksheets(1)for the first tab,Worksheets(2)for the second, and so on. Index refers to tab order, so it shifts if someone moves a sheet. - Use the Worksheets collection versus Sheets collection.
Worksheetscontains only worksheet tabs.Sheetsalso includes chart sheets. If your workbook has chart sheets,For Each ws In ThisWorkbook.Sheetswill fail when it hits a chart sheet because a chart sheet is not a Worksheet. Loop overWorksheetsunless you specifically need chart sheets. - Release the reference when done. Add
Set ws = Nothingat the end of the routine. This is good practice in long macros and helps avoid lingering references. - Run and test. Press F5 with the cursor inside the subroutine. If you get a compile error, check that the variable is declared before use and that you used
Setfor the assignment.
Common Mistakes
- Assigning without Set. Writing
ws = ThisWorkbook.Worksheets("Sheet1")causes "Object variable or With block variable not set" or a compile error. Object variables always needSet. - Using the wrong sheet name. The name in
Worksheets("Sheet1")is the tab name, not the VBA code name shown in the Project Explorer. They often differ. Check the tab itself. - Assuming the active sheet. Code like
Range("A1").Value = 1without a sheet qualifier writes to whatever sheet is active, which may not be the one you expect. Qualify with a Worksheet variable. - Looping over Sheets instead of Worksheets. As noted above, chart sheets break a
For Each ws In Sheetsloop. UseWorksheets. - Forgetting that a closed workbook has no Worksheet objects. If you reference a workbook that is not open, you must open it first, or the reference fails.
Tips
- Use a
With wsblock to avoid repeating the variable name:With ws....Range("A1").Value = 1...End With. - Name your variables clearly, such as
wsDataorwsReport, especially when you work with several sheets in one macro. - Use
ThisWorkbookfor the workbook containing the macro, andActiveWorkbookonly when you deliberately mean whichever workbook is currently active. - To check whether a sheet exists before assigning it, loop through
Worksheetsand comparews.Nameto the target name. Assign only if found, and show a message if not. - Keep sheet names stable. If you rename a tab, any hard-coded name in code must be updated too.
Troubleshooting
- Run-time error 9: Subscript out of range. The sheet name does not match any tab, or the workbook reference is wrong. Verify the exact tab name, including spaces and capitalization (names are not case-sensitive, but spelling must match).
- Run-time error 91: Object variable or With block variable not set. You declared the variable but never assigned it with
Set, or the assignment failed. Add theSetline and confirm the sheet exists. - Compile error: Expected: end of statement. Often caused by missing
Setor by using a reserved word as a variable name. Rename the variable. - Macro runs but changes the wrong sheet. You likely used an unqualified
RangeorCellscall. Qualify every range with the Worksheet variable. - Loop stops on a chart sheet. Switch the loop to
ThisWorkbook.Worksheetsinstead ofSheets. - Code works on one machine but not another. Check that the workbook is saved in a macro-enabled format (.xlsm) and that macro security settings allow it to run.
Frequently Asked Questions
What is the difference between Worksheet and Sheets in VBA?
Worksheets is a collection of only worksheet tabs. Sheets includes worksheets plus chart sheets and other sheet types. Declare variables as Worksheet when you loop over Worksheets.
Can I use a Worksheet variable without Set?
No. Object variables, including Worksheet, must be assigned with Set. Without it you get an error or an empty reference.
How do I refer to a sheet by its code name instead of its tab name?
Use the code name shown in the Project Explorer, for example Sheet1.Range("A1").Value. The code name stays the same even if the tab is renamed, which makes it more robust.
Why does my macro say the sheet does not exist when I can see the tab?
The name in the code probably does not match the tab exactly, or the code is looking in a different workbook. Check for leading or trailing spaces in the tab name and confirm you are using ThisWorkbook or the correct workbook reference.
Can I store multiple worksheets in one variable?
No, a Worksheet variable holds one sheet at a time. To work with several, use a loop, an array of Worksheet objects, or a Collection.
Do I need to set the variable to Nothing?
It is not strictly required in most macros, but it is good practice in longer routines and when you want to release the reference explicitly. Add Set ws = Nothing at the end.