Excel VBA Range Type: What It Is and How to Use It
In this article
A Range in Excel VBA is the object type that represents a cell, a group of cells, a row, a column, or a non-contiguous selection on a worksheet. You declare it as Dim rng As Range, then point it at real cells with Set rng = Worksheets("Sheet1").Range("A1:C10"). The single most common error is writing rng = Range("A1") instead of Set rng = Range("A1") — Range is an object, so it needs Set. This guide walks through declaring, assigning, reading from, writing to, and resizing a Range, plus the mistakes that trip up almost everyone the first few times.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
- A workbook saved as .xlsm (macro-enabled) so your code survives closing the file.
- Basic familiarity with the VBA Editor: press Alt+F11 on Windows, or Option+F11 on Mac, to open it.
- At least one worksheet with some data in it — a few values in A1:C10 is enough to follow along.
Step-by-Step
- Open the VBA Editor and insert a module. In the VBA Editor, use Insert > Module. A blank code window appears. Everything below goes in this module. Menu labels can differ slightly between Excel versions and between Windows and Mac, but Insert > Module has been stable for many releases.
- Declare a Range variable. Type
Dim rng As Range. This tells VBA the variable will hold a Range object. You can also declare several at once:Dim rng As Range, cell As Range. AvoidDim rngwithout a type — it becomes a Variant and you lose the editor's autocomplete and type checking. - Assign a range to the variable with Set. Type
Set rng = Worksheets("Sheet1").Range("A1:C10"). TheSetkeyword is mandatory for objects. Qualifying the worksheet avoids ambiguity about which sheet the range lives on — if you omit it, VBA uses the active sheet, which changes as the user clicks around. - Read a value out of the range. Use
MsgBox rng.Valueto see the top-left cell's value, orMsgBox rng.Cells(2, 3).Valueto read the cell in row 2, column 3 of the range (that is C2 if the range starts at A1)..Valuereturns a Variant that is a 2D array when the range has more than one cell, so for bulk reads assign to a Variant and loop. - Write a value into the range. Use
rng.Value = 100to fill every cell in the range with 100, orrng.Cells(1, 1).Value = "Hello"to write only the first cell. Writing to a multi-cell range with a single value broadcasts that value to all cells — handy for clearing or filling, dangerous if you meant one cell. - Resize and offset without re-typing addresses.
Set rng = rng.Resize(5, 3)makes the range 5 rows tall and 3 columns wide starting from its current top-left cell.Set rng = rng.Offset(1, 0)moves it down one row. These two methods let you build dynamic ranges from a starting anchor instead of hard-coding addresses. - Find the last used row to build a dynamic range. A common pattern:
Dim lastRow As Long, thenlastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row, thenSet rng = Worksheets("Sheet1").Range("A1:A" & lastRow). This sizes the range to the data instead of a fixed block. Note thatEnd(xlUp)behaves like Ctrl+Up and stops at the first blank above the bottom of the sheet, so it assumes no gaps in column A. - Loop through the cells if you need per-cell logic.
For Each cell In rng…Next cellvisits every cell. For large ranges this is slow; readingrng.Valueinto a Variant array and looping the array is dramatically faster. UseFor Eachfor readability on small ranges and the array approach when the range is thousands of cells. - Test with F5 or F8. Press F5 to run the whole procedure, or F8 to step through one line at a time and watch the Locals window (View > Locals) to confirm
rngpoints where you expect. IfrngshowsNothing, theSetline did not execute or failed silently.
Common Mistakes
- Omitting Set.
rng = Range("A1")raises "Object variable or With block variable not set" (error 91) or assigns the cell's default value instead of the object. Always useSetfor objects. - Forgetting the worksheet qualifier. Unqualified
Range("A1")refers to the active sheet. If the user switches sheets mid-run, your code reads or writes the wrong place. Qualify withWorksheets("Name")or use a worksheet variable. - Assuming .Value is a single value. For a multi-cell range,
.Valueis a 2D array. Comparing it directly to a number or string fails; index it asarr(1, 1). - Using Range on a closed workbook or a sheet name with a typo. This raises "Subscript out of range" (error 9). Check the sheet tab name exactly, including spaces.
- Confusing Range with Cells.
Cells(1, 1)is also a Range, butRange("A1")andCells(1, 1)are written differently. Both return Range objects; pick one style and stay consistent. - Hard-coding the last row.
Range("A1:A1000")breaks when data grows past row 1000 or shrinks. Use theEnd(xlUp)pattern instead.
Tips
- Use
Option Explicitat the top of the module. It forces you to declare every variable, which catches typos likernggbefore they become runtime errors. - Name your ranges in the worksheet (Formulas > Define Name) and refer to them as
Range("MyData"). If the data moves, the name follows it. - Use
rng.Addressin a debug message to confirm exactly which cells a variable points at — invaluable when a range is built dynamically. - For bulk operations, read
rng.Valueinto a Variant array, process the array, then write it back in one assignment. This is far faster than touching cells one at a time. - Set object variables to
Nothingat the end of long-running procedures to release references, especially in loops that create many ranges. - Use
rng.EntireRoworrng.EntireColumnwhen you need the whole row or column that a range touches, rather than computing the address yourself.
Troubleshooting
Error 91, "Object variable or With block variable not set." You used rng = ... instead of Set rng = ..., or the Set line never ran because of an earlier error. Check the line above the failure and add Set.
Error 9, "Subscript out of range." The worksheet name in Worksheets("...") does not match a tab in the active workbook. Verify spelling and spacing, and confirm the workbook containing the sheet is the one your code is running against.
Error 1004, "Application-defined or object-defined error." Usually a bad address string, a range that extends past the sheet limits, or a reference to a protected sheet. Check the address, unprotect the sheet if you have permission, and confirm the sheet is not hidden in a way that blocks access.
The code runs but writes to the wrong sheet. You omitted the worksheet qualifier, so VBA used the active sheet. Add Worksheets("Name") in front of Range.
The macro is slow on large ranges. You are looping cell by cell. Read the range into a Variant array, process the array, and write it back. Also set Application.ScreenUpdating = False at the start and restore it to True at the end.
If that did not work, step through with F8 and watch the Locals window. Confirm rng is not Nothing and that rng.Address matches what you expect. If it does not, the problem is in how the range was built, not in what you do with it.
Frequently Asked Questions
What is the difference between Range and Cells in VBA?
Both return Range objects. Range("A1") takes an address string; Cells(row, column) takes numeric coordinates. Cells is convenient in loops because you can compute row and column numbers, while Range is clearer for fixed blocks like "A1:C10".
Why does my Range variable show Nothing?
Because the Set statement did not execute or failed. Common causes are a typo in the sheet name, a missing Set keyword, or an earlier error that stopped the procedure before the assignment line.
Can I store a Range in an array?
Yes, but you usually do not need to. Assigning rng.Value to a Variant gives you a 2D array of the values, which is faster to process than an array of Range objects. Use an array of Range objects only when you need per-cell formatting or addresses.
How do I refer to a range on a different worksheet?
Qualify it: Worksheets("Sheet2").Range("A1:B10"). If the workbook is not the active one, also qualify the workbook: Workbooks("Book1.xlsx").Worksheets("Sheet2").Range("A1:B10"). The workbook must be open for this to work.
What does Resize do?
rng.Resize(rows, columns) returns a new range of the given size whose top-left cell is the same as rng's top-left cell. It does not move the original range; you must assign the result with Set if you want to keep it.
Does Range work on protected sheets?
Reading values works if the sheet is protected but cells are not locked. Writing to locked cells fails with error 1004. You need the sheet unprotected, or the cells unlocked, before your code can change them.