Range and Cells: referring to cells in VBA
Almost every macro reads or writes cells. There are two ways to point at them: Range with the address you know from Excel, and Cells with a row and column number. Here you learn both, when each one fits better, and how to work with a whole block of cells at once.
In short: Range("F2") and Cells(2, 6) are the same cell. Cells takes the row first, then the column. A block of cells is Range("A2:C6") or Range(Cells(2, 1), Cells(6, 3)).
What you need to know: VBA basics (Sub, assignment with =, Value). Recording a macro helps but is not required.
Try it right now
Row 4 of the phone list holds the person who is covering for a colleague. Use Cells to write their name and extension to E2 and F2.
Task The person in row 4 is covering for a colleague. Use Cells(row, column) to write their name (column 1) to E2 and their extension (column 3) to F2.
| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Name | Department | Extension | Cover | Extension | |
| 2 | Sarah Collins | Purchasing | 214 | |||
| 3 | Raj Patel | Sales | 231 | |||
| 4 | Tom Fletcher | Accounts | 247 | |||
| 5 | Megan Hughes | HR | 252 | |||
| 6 | Daniel Reyes | IT | 268 |
Range: the address as text
Range takes an address in quotes, written just as it would appear in an Excel formula:
Range("B2").Value = 250 ' one cell
Range("A2:C6").Value = "" ' a block, all cells emptied
Range("D2:D6").Value = "open" ' the same text in five cells
Assign a single value to a range and every cell in it gets that value. Range reads well and is the first choice as long as the address is fixed.
Cells: row and column as numbers
Cells takes two numbers: row first, then column. Column A is 1, B is 2, F is 6.
Cells(2, 6).Value = Cells(4, 1).Value ' F2 gets the contents of A4
Cells(2, "F").Value = 247 ' the column can be a letter too
| Syntax | Cell | Good for |
|---|---|---|
Range("F2") | F2 | A fixed cell, easy to read |
Cells(2, 6) | F2 | Row or column is calculated, for example in a loop |
Cells(2, "F") | F2 | Row calculated, column fixed |
Range("A2:C6") | A2 to C6 | A fixed block |
Range(Cells(2, 1), Cells(6, 3)) | A2 to C6 | A block whose corners are calculated |
Why two ways? With Range you have to build the address as text whenever it changes. With Cells you just drop in a variable: Cells(row, 1). Once you work through a list with loops, that is the usual way.
Blocks with Range(Cells(…), Cells(…))
Instead of an address, Range can also take two cells. The range is then the rectangle from the first cell to the second, so Range(Cells(2, 1), Cells(6, 3)) is A2:C6. Blocks of the same size can be transferred in one assignment:
Range(Cells(2, 5), Cells(6, 7)).Value = Range(Cells(2, 1), Cells(6, 3)).Value
That copies all 15 values from A2:C6 to E2:G6, without the clipboard and without formats.
One more exercise: archive a block
The branch figures for the month go to the archive in one step. Write both ranges with Cells.
Task This month's figures in A2:C6 go to the archive E2:G6 in one step. Write both ranges as Range(Cells(…), Cells(…)), using the first and the last cell.
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Branch | Sales | Customers | Branch | Sales | Customers | |
| 2 | Boston | 48,210 | 1312 | ||||
| 3 | Leeds | 39,870 | 1104 | ||||
| 4 | Denver | 52,640 | 1468 | ||||
| 5 | Bristol | 27,450 | 802 | ||||
| 6 | Atlanta | 41,390 | 1187 |
Value, and why people leave it out
Value is the contents of a cell. It is the default property of a Range, which is why Range("B2") = 5 works too. With .Value, though, it is obvious that you mean the contents and not the formula or the format, so this course always writes it.
Range and Cells without a sheet name refer to the active sheet. How to address a specific sheet, as in Worksheets("Archive").Range("A1"), is covered in Worksheets.
Find the bug
A classic: when you think "F2", the column comes first. Cells wants it the other way round.
Task Find the bug: the macro should write the two quarterly figures of the North region (row 3) next to the report, Q1 to F2 and Q2 to F3. Instead the numbers end up somewhere else. Fix the targets.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Region | Quarter 1 | Quarter 2 | Report | |
| 2 | South | 128,400 | 131,950 | North, Q1 | |
| 3 | North | 96,300 | 102,780 | North, Q2 | |
| 4 | West | 114,250 | 109,600 | ||
| 5 | East | 87,120 | 93,410 |
In real Excel
When you type Cells( in the VBA editor, a small tooltip shows the arguments. To see which cell a Cells(…) actually hits, step through: F8 runs the macro line by line, and after each line you see which cell changed. In the Immediate window (Ctrl+G), ?Cells(2, 6).Address prints the address, here $F$2.
Common mistakes
Row and column swapped
Cells(6, 2) is B6, not F2. The exercise spots this and says: "The values are mirrored: Cells takes the row first, then the column. Cells(2, 1) is A2, not B1." To remember it: as in R1C1, row before column.
Missing quotes
Range(A4) without quotes looks for a variable called A4, which is empty. The exercise stops with "Run-time error '1004': Method 'Range' of object '_Global' failed"; Excel also reports a run-time error 1004. The address is text and belongs in "…".
Ranges of different sizes
If the target is bigger than the source, Excel fills the extra cells with #N/A. End the source at Cells(6, 2) instead of Cells(6, 3) and the exercise reports that G2 contains #N/A where 1312 is expected. Count the rows and columns of both ranges.
Typing values instead of reading cells
Range("E2").Value = "Tom Fletcher" is only right for the data you can see. Check also runs with other names and then tells you it only works on the visible data. Read the value from the cell.
FAQ
Is Range or Cells faster?
The result is the same; both give you the same cell. Pick whatever reads better: Range for fixed addresses, Cells when the row or column comes from a variable.
How do I refer to a whole column?
With Range("C:C") or Columns(3); a whole row with Range("2:2") or Rows(2). For lists that is usually too much: better go only as far as the last used row, as shown in Find the last row.
What is column 27 called?
AA. After Z (26) the names continue with two letters. With Cells you do not need to convert: Cells(1, 27) is AA1.
Do I always have to write .Value?
No, but it makes the code clearer. Without .Value it is not obvious at first glance whether you mean the cell itself or its contents.