Excel VBA Worksheet Range: How to Use It
In this article
You use the Excel VBA Worksheet.Range property to refer to a cell or block of cells on a specific worksheet so your macro can read, write, format, or act on them. The shortest working pattern is Worksheets("Sheet1").Range("A1").Value = "Hello", which writes to cell A1 on Sheet1. Everything else in this guide builds on that one line.
What You'll Need
- Excel for Windows or Mac with macros enabled. The steps below use the Windows ribbon layout; Mac labels are similar but can sit under slightly different tabs.
- A workbook saved as
.xlsm(macro-enabled) if you want to keep the code. A normal.xlsxfile will drop your macros when you save. - The Developer tab visible. If it is not, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows; on Mac use the Developer tab > Visual Basic).
Menu and ribbon labels can differ slightly by Excel version, device, and region, so if a name looks different, look for the closest match.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or use Developer > Visual Basic (Mac). The editor opens in a separate window.
- Insert a module. In the editor menu choose Insert > Module. A blank code pane appears. Code in a standard module can be run with F5 or from the Macro dialog.
- Qualify the worksheet. Always name the sheet before
.Range. For example:Worksheets("Data").Range("A1").Value = 100
Using the sheet's tab name (the Name property) is safer than the tab caption, because renaming the tab breaks code that uses the caption. You can set the name in the Properties window (F4) while the sheet is selected. - Refer to a single cell. Use A1 notation inside quotes:
Range("B2"). To read the value into a variable:Dim x As Variant : x = Worksheets("Data").Range("B2").Value. - Refer to a block of cells. Use the top-left and bottom-right corners separated by a colon:
Range("A1:C10"). This is the same block you would select by dragging from A1 to C10. - Use Cells for calculated positions. When row or column numbers come from variables, use
Cells(row, column):Worksheets("Data").Cells(5, 3).Value = "Total"writes to C5. To build a block, combine them withRange(Cells(1,1), Cells(10,3)). - Act on the whole block at once. Many operations accept a multi-cell range directly. For example,
Range("A1:C10").ClearContentsempties the block, andRange("A1:C10").Font.Bold = Truebolds every cell in it. - Copy or move values.
Range("A1:A10").Copy Destination:=Range("D1")copies the block to D1:D10. Use.Cutinstead of.Copyto move it. - Loop over the cells. To process each cell, use
For Each c In Range("A1:A10")and readc.Value. This is clearer than indexing with row numbers and avoids off-by-one errors. - Run and test. Press F5 with the cursor inside the routine, or use Developer > Macros. Check the target sheet to confirm the values landed where you expected.
- Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Close and reopen once to confirm the macro survived.
Common Mistakes
- Leaving off the worksheet. Writing
Range("A1")on its own refers to the active sheet, which may not be the one you meant. Always qualify withWorksheets("Name"). - Confusing tab caption with sheet name. The tab text you see is the Caption; code uses the Name. They are often the same, but not always. Check the Properties window if code fails after a rename.
- Using quotes around variables.
Range("A" & i)works;Range("Ai")does not. Concatenate with&. - Assuming Range means the used area.
Range("A1")is one cell, not the whole data region. UseCurrentRegionorUsedRangeif you need the surrounding block, and check the result before relying on it. - Forgetting the sheet exists. A misspelled sheet name raises a subscript out of range error at runtime. Match the name exactly, including spaces.
- Saving as .xlsx. The code disappears on save. Keep the workbook as .xlsm.
Tips
- Use a
Withblock to avoid repeating the sheet reference:With Worksheets("Data")then.Range("A1").Value = 1inside it. - Prefer
Value2overValuewhen reading numbers; it avoids currency and date type conversion surprises. - Read a whole block into a Variant array in one go for speed:
arr = Range("A1:C100").Value2. Looping cell by cell is slow on large blocks. - Turn off
ScreenUpdatingat the start of a long macro and back on at the end to keep the screen from flickering. - Name your data blocks with Formulas > Define Name and refer to them in code by name; the code survives rows being inserted.
- Use
OffsetandResizefor positions relative to a known cell rather than hard-coding addresses.
Troubleshooting
- Subscript out of range (Error 9): the sheet name in the code does not match any sheet. Check the Name property, not just the tab text.
- Application-defined or object-defined error (Error 1004): usually a bad address string, a missing quote, or a reference to a sheet that is not active when the code expects it. Step through with F8 to find the failing line.
- Values land on the wrong sheet: the code omitted the worksheet qualifier and wrote to whichever sheet was active. Add
Worksheets("Name"). - Macro does not appear in the list: the code may be in a sheet module instead of a standard module, or the file was saved as .xlsx. Move the code to a module and re-save as .xlsm.
- Macros are blocked: check the Trust Center settings under File > Options > Trust Center > Trust Center Settings > Macro Settings. Only enable macros from files you trust.
- If that did not work: close and reopen Excel, then re-check the sheet name and the exact address string. If the workbook still misbehaves, copy the code into a fresh .xlsm workbook and test there before changing anything else.
Frequently Asked Questions
What is the difference between Range and Cells in VBA?
Range takes an address string like "A1" or "A1:C10". Cells takes row and column numbers, which suits loops and calculated positions. Both refer to cells on the worksheet you qualify.
Can I use Range without naming the worksheet?
Yes, but it then applies to the active sheet, which can change while the macro runs. Naming the sheet is the reliable habit.
How do I refer to a range on a sheet whose tab name has a space?
Put the name in quotes exactly as it appears: Worksheets("Sales Data").Range("A1"). The space is fine inside the quotes.
Why does my macro work once and then fail the second time?
The active sheet or selection probably changed between runs. Qualify every range with its worksheet and avoid relying on Selection or ActiveSheet.
Does Range work the same on Excel for Mac?
The object model is the same, so Worksheets("Name").Range("A1") behaves the same way. Only the editor's menus and shortcuts differ slightly.
Can I write to a protected sheet with Range?
Not while protection is on. Unprotect the sheet in code with the correct password, do the write, then re-protect it. Do not store passwords in plain text in the workbook.