Smart Systems for Home

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

Excel VBA Range Type: What It Is and How to Use It

· 7 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

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

  1. 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.
  2. 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. Avoid Dim rng without a type — it becomes a Variant and you lose the editor's autocomplete and type checking.
  3. Assign a range to the variable with Set. Type Set rng = Worksheets("Sheet1").Range("A1:C10"). The Set keyword 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.
  4. Read a value out of the range. Use MsgBox rng.Value to see the top-left cell's value, or MsgBox rng.Cells(2, 3).Value to read the cell in row 2, column 3 of the range (that is C2 if the range starts at A1). .Value returns 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.
  5. Write a value into the range. Use rng.Value = 100 to fill every cell in the range with 100, or rng.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.
  6. 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.
  7. Find the last used row to build a dynamic range. A common pattern: Dim lastRow As Long, then lastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row, then Set rng = Worksheets("Sheet1").Range("A1:A" & lastRow). This sizes the range to the data instead of a fixed block. Note that End(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.
  8. Loop through the cells if you need per-cell logic. For Each cell In rng … Next cell visits every cell. For large ranges this is slow; reading rng.Value into a Variant array and looping the array is dramatically faster. Use For Each for readability on small ranges and the array approach when the range is thousands of cells.
  9. 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 rng points where you expect. If rng shows Nothing, the Set line 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 use Set for 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 with Worksheets("Name") or use a worksheet variable.
  • Assuming .Value is a single value. For a multi-cell range, .Value is a 2D array. Comparing it directly to a number or string fails; index it as arr(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, but Range("A1") and Cells(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 the End(xlUp) pattern instead.

Tips

  • Use Option Explicit at the top of the module. It forces you to declare every variable, which catches typos like rngg before 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.Address in a debug message to confirm exactly which cells a variable points at — invaluable when a range is built dynamically.
  • For bulk operations, read rng.Value into 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 Nothing at the end of long-running procedures to release references, especially in loops that create many ranges.
  • Use rng.EntireRow or rng.EntireColumn when 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.