Excel VBA Cells X Y: Referencing Cells by Row and Column
In this article
In Excel VBA, you reference a cell by its row and column numbers using the Cells(row, column) property — for example, Cells(3, 2) points to B3. That is the short answer. This guide walks through the syntax, how to loop with it, how to combine it with ranges, and the errors that trip people up. No add-ins are needed; the Visual Basic Editor is built into every desktop version of Excel.
What You'll Need
- Excel for Windows or Mac (desktop). The VBA editor is not available in Excel for the web.
- A workbook saved as
.xlsm(macro-enabled) if you want to keep your code. - Basic familiarity with opening the editor: press
Alt+F11on Windows, orOption+F11on Mac. Menu labels in the editor can differ slightly by version and region.
Step-by-Step
- Open the Visual Basic Editor. In Excel, press
Alt+F11(Windows) orOption+F11(Mac). If the Project Explorer is not visible, use View > Project Explorer. - Insert a module. In the Project Explorer, right-click your workbook name and choose Insert > Module. Code typed into a sheet module also works, but a standard module keeps things tidy.
- Write a minimal procedure. Type the following, then click inside it and press
F5to run:
This writes to row 3, column 2 — that is cell B3. Note the order: row first, column second.Sub WriteToCell() Cells(3, 2).Value = "Hello" End SubCells(2, 3)would be C2, not B3. - Read a value back. Replace the line with
MsgBox Cells(3, 2).Valueto confirm what is in B3. Reading and writing use the same addressing. - Use variables for the coordinates. Hard-coded numbers are fine for a one-off, but variables make the code reusable:
Declare row and column variables asDim r As Long, c As Long r = 5 c = 1 Cells(r, c).Value = "Row 5, column A"Long; Excel supports over a million rows, andIntegeroverflows past 32,767. - Loop through a block. Nested loops are the standard pattern for filling a grid:
This fills A1:C10 with a multiplication table. The outer loop moves down rows, the inner loop moves across columns.Dim r As Long, c As Long For r = 1 To 10 For c = 1 To 3 Cells(r, c).Value = r * c Next c Next r - Qualify the sheet. Unqualified
Cellsrefers to the active sheet, which changes as users click around. Be explicit:
If the sheet name might change, refer to it by index or use a worksheet variable set withWorksheets("Data").Cells(2, 2).Value = 100Set ws = ThisWorkbook.Worksheets("Data"). - Combine with Range for a block.
Cellsreturns a single cell, but you can use it to build a range:
This clears A1:C10 in one statement. BothRange(Cells(1, 1), Cells(10, 3)).Value = 0Cellscalls must belong to the same sheet as theRangecall, or you get an error. - Use the last row to size loops. Hard-coded loop limits break when data grows. Find the last used row first:
This starts at row 2 (assuming row 1 is a header) and stops at the last filled cell in column A.Dim lastRow As Long lastRow = Worksheets("Data").Cells(Worksheets("Data").Rows.Count, 1).End(xlUp).Row For r = 2 To lastRow Cells(r, 3).Value = Cells(r, 1).Value + Cells(r, 2).Value Next r - Save as macro-enabled. Use File > Save As and choose
.xlsm. A plain.xlsxdiscards the code on close.
Common Mistakes
- Swapping row and column.
Cells(2, 5)is E2, not B5. Say it out loud as "row two, column five" while writing. - Using
Integerfor rows. It overflows above 32,767. UseLong. - Relying on the active sheet. Code that works in testing can write to the wrong sheet once a user clicks elsewhere. Qualify every reference.
- Mixing sheets in a range.
Range(Sheets("A").Cells(1,1), Sheets("B").Cells(2,2))fails. Keep both endpoints on one sheet. - Forgetting
.Value.Cells(1,1) = 5usually works becauseValueis the default property, but being explicit avoids confusion with formatting or formulas. - Assuming
CellsandRange("B3")behave identically. They both return a Range object, butCellsis easier to loop with andRangeis easier to read for fixed addresses.
Tips
- Use
Cellswhen the address is calculated; useRange("B3")when it is fixed and readability matters. - Turn on
Option Explicitat the top of the module (Tools > Options > Require Variable Declaration) to catch undeclared variables. - Step through code with
F8instead of running it all at once; you can watch each cell change. - For large loops, read the range into a Variant array, process it in memory, and write it back in one statement — far faster than touching cells one at a time.
- Use
Cells(r, c).Addressin aMsgBoxwhile debugging to confirm which cell you are actually hitting.
Troubleshooting
- Run-time error 1004. Usually an invalid sheet name, a reference to a closed workbook, or a range built from cells on two different sheets. Check the sheet name spelling and qualify both endpoints.
- Run-time error 9 (Subscript out of range). The worksheet name in your code does not exist in the active workbook. Verify it in the Project Explorer.
- Code writes to the wrong place. You are probably relying on the active sheet. Add the worksheet qualifier and rerun.
- Nothing happens. The macro may be disabled. Check File > Options > Trust Center > Trust Center Settings > Macro Settings; labels vary by version. Also confirm you pressed
F5with the cursor inside the correct procedure. - Loop runs forever or stops early. Print the loop bounds with
Debug.Print r, cand check the Immediate window (Ctrl+G). - Macro gone after saving. The file was saved as
.xlsx. Re-save as.xlsmand re-add the code.
Frequently Asked Questions
Is Cells(row, column) or Cells(column, row)?
Row first, then column. Cells(3, 2) is B3.
Can I use Cells with a named range?
Not directly. Cells takes numeric row and column indexes. To work with a named range, use Range("MyName") or convert with Range("MyName").Cells(1,1).
What is the maximum row and column number?
It depends on the file format. Modern .xlsx and .xlsm files support 1,048,576 rows and 16,384 columns (XFD). Older .xls files are far smaller. Use Rows.Count and Columns.Count rather than hard-coding limits.
Does Cells work on a protected sheet?
Reading works; writing fails unless the cells are unlocked or you unprotect the sheet in code. Unprotecting in code requires the password if one is set.
Can I use Cells in Excel for the web?
No. The VBA editor is not available in Excel for the web. Use desktop Excel to write and run macros.
Why does Cells(1,1) sometimes return a Range, not a value?
Because Cells always returns a Range object. Add .Value to get the contents, or .Formula to get the formula text.