Excel VBA Cells: How to Reference and Edit Ranges
In this article
Excel VBA's Cells object lets you read or write any single cell by row and column number, and it is the most reliable way to address cells in a macro because numbers never change when columns are inserted. The shortest working example is Range("A1").Value = Cells(1, 1).Value, but the practical pattern is Cells(rowNumber, columnNumber) inside a With block or a worksheet reference, for example Worksheets("Data").Cells(2, 3).Value = 100. Everything below builds from that one line.
What You'll Need
- A desktop copy of Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac). Labels differ slightly by version and region.
- A workbook saved as .xlsm (macro-enabled) if you want the code to persist.
- The VBA editor open: press Alt+F11 on Windows, or Fn+Option+F11 on a Mac.
- A blank module: in the editor choose Insert > Module.
Step-by-Step
- Create a test macro. In the module, type
Sub CellTest()and press Enter; the editor addsEnd Subautomatically. Put every line below between those two lines. - Write to a cell by row and column. Add
Cells(1, 1).Value = "Hello". The first number is the row, the second is the column, so this writes to A1.Cells(3, 2)is B3. Column letters are never used with this form. - Read a value back. Add
MsgBox Cells(1, 1).Value. Run the macro with F5 and a message box shows "Hello". This confirms the write worked before you build anything larger. - Always name the worksheet. An unqualified
Cellsrefers to whatever sheet is active, which breaks the moment a user clicks another tab. Change the lines toWorksheets("Sheet1").Cells(1, 1).Value = "Hello". Use the tab name exactly as it appears, including spaces. - Use a With block for repeated work. Instead of repeating the sheet name, write
With Worksheets("Sheet1")on one line, then.Cells(2, 1).Value = "Name"and.Cells(2, 2).Value = "Score", thenEnd With. The leading dot is required; without it the code silently points at the active sheet again. - Loop through a block of cells. Declare counters with
Dim r As Long, then useFor r = 2 To 10and inside the loopCells(r, 3).Value = Cells(r, 1).Value * Cells(r, 2).Value, closing withNext r. This fills C2:C10 with the product of columns A and B. UseLongrather thanInteger; Excel rows exceed the Integer limit. - Find the last used row instead of hard-coding it. Use
lastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End(xlUp).Row. This starts at the bottom of column A and jumps up to the last filled cell. Then loopFor r = 2 To lastRowso the macro adapts as data grows. - Format through the same reference.
Cells(1, 1).Font.Bold = Truebolds A1, andCells(1, 1).Interior.Color = RGB(255, 255, 0)gives it a yellow fill. Properties hang off the cell object exactly as they do off a Range. - Select a whole block when you need one.
Range(Cells(1, 1), Cells(10, 3)).Selecthighlights A1:C10. Note that selecting is rarely necessary; act directly on the range instead, because Select is slow and depends on the right sheet being active. - Run and save. Press F5, check the result on the sheet, then save the workbook as .xlsm. If Excel offers to save without macros, you chose the wrong file type.
Common Mistakes
The single biggest error is omitting the worksheet reference. Code that works on your machine fails on a colleague's because their active sheet is different. Always qualify with Worksheets("name") or ActiveSheet deliberately.
Reversing row and column is the second. Cells(5, 2) is B5, not E2. Say the order out loud as "row, then column" until it sticks.
Mixing up Cells and Range causes confusion too: Range("B5") takes a text address, Cells(5, 2) takes numbers, and Range(Cells(1,1), Cells(5,2)) combines them into a block. Forgetting the dot inside a With block is a third silent failure, and using Integer for row counters overflows on large sheets.
Finally, writing to a protected sheet raises an error. Unprotect it in code with the correct password, or design the macro to skip locked cells.
Tips
Use Cells when positions are calculated and Range when you know the address, then combine them for blocks. Turn off screen updating at the start of long loops with Application.ScreenUpdating = False and restore it to True before the macro ends, including in error handlers, or Excel appears frozen.
Read values into a VBA array first and write them back in one operation when processing thousands of rows; cell-by-cell access is dramatically slower. Name your counters meaningfully (rowIndex, colIndex) so the row/column order stays obvious months later.
Test on a copy of the workbook, and add Option Explicit at the top of the module so undeclared variables raise an error instead of defaulting silently.
Troubleshooting
If you get "Subscript out of range", the worksheet name in your code does not match any tab. Check spelling, trailing spaces and whether the sheet was renamed.
If "Application-defined or object-defined error" appears, you are usually referencing a cell outside the sheet, or the sheet is protected. Verify the row and column numbers are at least 1 and within limits.
If nothing happens at all, the macro may not be running: check the module is a standard module, not a sheet or ThisWorkbook module, and that macros are enabled in Trust Center settings. If the values land on the wrong sheet, you have an unqualified reference; add the worksheet name.
If results look stale, confirm calculation is automatic (Formulas > Calculation Options > Automatic) and that you are reading the cell after the write, not before.
Frequently Asked Questions
What is the difference between Cells and Range in VBA?
Cells(row, column) uses two numbers and always refers to a single cell. Range takes an address string or two cell references and can cover a block. They can be combined: Range(Cells(1,1), Cells(10,3)).
How do I refer to a cell on another worksheet?
Qualify it fully: Worksheets("Data").Cells(2, 1).Value. The sheet does not need to be active, which is why this approach is safer than selecting tabs.
Can I use column letters with Cells?
Not directly; it takes numbers only. Convert a letter with Range("C1").Column, which returns 3, or just use Range("C1") when you already know the letter.
Why does my macro write to the wrong sheet?
Because the reference is unqualified and Excel used the active sheet. Add the worksheet name before Cells, and include the leading dot inside any With block.
How do I loop through every used row?
Find the last row with Cells(Rows.Count, 1).End(xlUp).Row, then use For r = 2 To lastRow and reference Cells(r, columnNumber) inside the loop.