Smart Systems for Home

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

Excel VBA Cells X Y: Referencing Cells by Row and Column

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

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+F11 on Windows, or Option+F11 on Mac. Menu labels in the editor can differ slightly by version and region.

Step-by-Step

  1. Open the Visual Basic Editor. In Excel, press Alt+F11 (Windows) or Option+F11 (Mac). If the Project Explorer is not visible, use View > Project Explorer.
  2. 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.
  3. Write a minimal procedure. Type the following, then click inside it and press F5 to run:
    Sub WriteToCell()
        Cells(3, 2).Value = "Hello"
    End Sub
    This writes to row 3, column 2 — that is cell B3. Note the order: row first, column second. Cells(2, 3) would be C2, not B3.
  4. Read a value back. Replace the line with MsgBox Cells(3, 2).Value to confirm what is in B3. Reading and writing use the same addressing.
  5. Use variables for the coordinates. Hard-coded numbers are fine for a one-off, but variables make the code reusable:
    Dim r As Long, c As Long
    r = 5
    c = 1
    Cells(r, c).Value = "Row 5, column A"
    Declare row and column variables as Long; Excel supports over a million rows, and Integer overflows past 32,767.
  6. Loop through a block. Nested loops are the standard pattern for filling a grid:
    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
    This fills A1:C10 with a multiplication table. The outer loop moves down rows, the inner loop moves across columns.
  7. Qualify the sheet. Unqualified Cells refers to the active sheet, which changes as users click around. Be explicit:
    Worksheets("Data").Cells(2, 2).Value = 100
    If the sheet name might change, refer to it by index or use a worksheet variable set with Set ws = ThisWorkbook.Worksheets("Data").
  8. Combine with Range for a block. Cells returns a single cell, but you can use it to build a range:
    Range(Cells(1, 1), Cells(10, 3)).Value = 0
    This clears A1:C10 in one statement. Both Cells calls must belong to the same sheet as the Range call, or you get an error.
  9. Use the last row to size loops. Hard-coded loop limits break when data grows. Find the last used row first:
    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
    This starts at row 2 (assuming row 1 is a header) and stops at the last filled cell in column A.
  10. Save as macro-enabled. Use File > Save As and choose .xlsm. A plain .xlsx discards 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 Integer for rows. It overflows above 32,767. Use Long.
  • 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) = 5 usually works because Value is the default property, but being explicit avoids confusion with formatting or formulas.
  • Assuming Cells and Range("B3") behave identically. They both return a Range object, but Cells is easier to loop with and Range is easier to read for fixed addresses.

Tips

  • Use Cells when the address is calculated; use Range("B3") when it is fixed and readability matters.
  • Turn on Option Explicit at the top of the module (Tools > Options > Require Variable Declaration) to catch undeclared variables.
  • Step through code with F8 instead 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).Address in a MsgBox while 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 F5 with the cursor inside the correct procedure.
  • Loop runs forever or stops early. Print the loop bounds with Debug.Print r, c and check the Immediate window (Ctrl+G).
  • Macro gone after saving. The file was saved as .xlsx. Re-save as .xlsm and 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.