Smart Systems for Home

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

Excel VBA Range with Variable Row and Column

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

To build a range with a variable row and column in Excel VBA, use the Cells property with numeric variables or build an address string and pass it to Range. The shortest working pattern is Set rng = ws.Cells(rowVar, colVar) for one cell, or Set rng = ws.Range(ws.Cells(r1, c1), ws.Cells(r2, c2)) for a block. This lets loops, counters and user input drive which cells your macro reads or writes, instead of hard-coded addresses like Range("B5").

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 region.
  • A workbook saved as .xlsm if you want to keep the macro.
  • Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module.

Step-by-Step

  1. Declare your variables. At the top of the procedure, declare row and column variables as Long: Dim r As Long, c As Long. Long is the correct type for row numbers because Excel supports more than 32,000 rows and Integer overflows above that.
  2. Set a worksheet reference. Use Dim ws As Worksheet then Set ws = ThisWorkbook.Worksheets("Sheet1"). Qualifying every range with a worksheet stops the macro from acting on whichever sheet happens to be active.
  3. Assign values to the row and column variables. For example r = 5 and c = 2. In a loop, let the counter supply them: For r = 2 To 10.
  4. Reference a single cell with Cells. Write ws.Cells(r, c).Value = "Hello". Cells takes the row number first and the column number second, which is the reverse of how A1 addresses read.
  5. Reference a block with two Cells calls. Use Set rng = ws.Range(ws.Cells(r1, c1), ws.Cells(r2, c2)). This is the safest way to build a dynamic block because it avoids string concatenation errors.
  6. Or build an address string for Range. If you prefer A1 notation, concatenate: ws.Range("A" & r & ":C" & r) selects columns A to C on row r. Use & to join text and numbers; VBA converts the number automatically.
  7. Assign the range to an object variable. Declare Dim rng As Range, then Set rng = .... Use Set whenever the target is an object; omitting it is a common source of runtime error 91.
  8. Read or write through the variable. rng.Value = 100 writes to every cell in the block. MsgBox rng.Address confirms which cells you actually captured before you change data.
  9. Test with a small loop. Run a loop from row 2 to 10 that writes the row number into column A, then step through with F8 to watch each assignment. This proves the variable wiring before you scale it up.

Common Mistakes

  • Swapping row and column. Cells(2, 5) is E2, not B5. Always pass row first.
  • Forgetting Set. rng = ws.Range(...) fails; object variables need Set rng = ....
  • Using Integer for row numbers. Integer tops out at 32,767, so long sheets throw overflow errors. Use Long.
  • Leaving the worksheet unqualified. Range("A" & r) without ws. points at the active sheet and silently writes to the wrong place.
  • Building addresses with missing quotes. Range(A & r) is invalid; column letters must be inside quotation marks.
  • Assuming row 1 is data. If your sheet has headers, start loops at row 2 or you will overwrite them.

Tips

  • Use ws.Cells(ws.Rows.Count, c).End(xlUp).Row to find the last used row dynamically, then feed that into your range.
  • Use ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column to find the last used column.
  • Prefer the two-Cells form over string concatenation when either coordinate may be zero or invalid, because it raises a clear error instead of building a nonsense address.
  • Wrap writes in Application.ScreenUpdating = False and restore it to True at the end for faster loops.
  • Use Offset relative to a variable cell when you need a neighbouring cell: ws.Cells(r, c).Offset(0, 1) is one column to the right.

Troubleshooting

If you get runtime error 1004, the address is usually invalid: check that row and column are at least 1 and that no variable is empty. If error 91 appears, a missing Set or an unassigned worksheet is the likely cause. If the macro writes to the wrong sheet, add the worksheet qualifier. If values land in the wrong cells, print rng.Address with a message box to see exactly what was captured. If the loop stops early, confirm the upper bound of the loop is a real row that contains data.

Frequently Asked Questions

Can I use a variable inside Range("A1")?

Not directly. Build the string with concatenation, for example Range("A" & r), or use Cells(r, 1).

What is the difference between Range and Cells?

Range takes an A1-style address; Cells takes a row number and column number. Cells is easier when both coordinates are variables.

How do I select a whole row with a variable number?

Use ws.Rows(r) or ws.Range(ws.Cells(r, 1), ws.Cells(r, ws.Columns.Count)).

Why does my variable range only affect one cell?

You probably assigned a single Cells call. For a block, pass two Cells calls to Range.

Can I use a variable for the sheet name too?

Yes. Set ws = ThisWorkbook.Worksheets(sheetNameVar) and then qualify every range with ws.

Do these steps work on Mac?

Yes, the VBA object model is the same; only the keyboard shortcuts for opening the editor differ.