Skip to the stories
Excel & Power BI

VBA Variables: Use Cells(i, 1) to Write to Changing Rows

A VBA variable can change which worksheet row a macro writes to. See how i increments and why repeating the same assignment produces different results.

VBA Variables: Use Cells(i, 1) to Write to Changing Rows
I’ll expand the existing explanation with practical context about variable scope, worksheet targeting, and the limits of the repeated-code example, while preserving the supplied HTML elements and link attributes.

From recorded addresses to changeable positions

A macro that writes to a fixed address can only update that address until someone edits its code. For a one-off task, that may be all you need; for a procedure that must work at different positions, the address becomes a constraint. A VBA variable gives the macro a value it can reuse and change while it runs, so the same assignment can target different worksheet rows.

Consider a macro recorded with a fixed cell reference:

Sub WriteInA2()
 Range("A2") = 10
End Sub

Each time this procedure runs, it puts 10 in A2. That’s predictable, but it doesn’t adapt to a different row. The Macro Recorder commonly produces this kind of fixed reference because it records the cells selected or changed, rather than inferring a broader rule about where data should go. To make a procedure work with changing positions, the code needs a way to represent a row that can take different values.

That change in approach matters when the task depends on a position calculated during the macro run. A fixed address describes one destination; a variable can hold a position that the procedure updates as it works. The code still needs a clear rule for choosing that position, but the address no longer has to be hard-coded into every assignment.

Use a variable as a row number

In this example, the variable i starts at 1. The macro uses it as the row number in Cells(i, 1) and also as the value to write:

image excel exercises
Sub WriteThreeCells()
 i = 1
 Cells(i, 1) = i
 i = i + 1
 Cells(i, 1) = i
 i = i + 1
 Cells(i, 1) = i
End Sub

The first argument in Cells(i, 1) identifies the row; the second identifies the column. Here, column 1 is column A. When i is 1, the assignment is effectively Cells(1, 1) = 1, so the macro writes 1 in A1. The same form can refer to a different row as soon as the value of i changes.

After the macro adds 1 to i, the next assignment uses row 2 and writes 2 in A2. The second increment advances the value again, so the final assignment writes 3 in A3. The assignment line is identical each time; its result changes because the variable has a different value at each point in the procedure. That separation between the instruction and its current value is what makes the code adaptable.

  • First assignment: i is 1, so the macro writes 1 in A1.
  • Second assignment: i is 2, so the macro writes 2 in A2.
  • Third assignment: i is 3, so the macro writes 3 in A3.
VBA code that writes the same line three times with the variable i, and the result 1, 2, 3 in cells A1 to A3

What the increment does

The statement i = i + 1 can look circular at first because i appears on both sides. Read it as an assignment that calculates a new value. VBA evaluates the right side using the current value of i, adds 1, then stores that result back in i. The old value is replaced.

If i currently contains 1, the calculation on the right is 1 + 1. The result, 2, replaces the old value on the left. This is called incrementing a value. It lets the procedure keep track of its position as it runs, and each later statement can use the updated value.

image excel quizzes

The distinction between reading and updating matters when following code. In Cells(i, 1) = i, the variable’s current value serves two purposes: it selects the row and supplies the value written into that row. In i = i + 1, the current value is used to calculate the next one. First the macro uses the current position; then it advances it.

In a larger procedure, it helps to track the value after every assignment rather than trying to infer the final result at a glance. Here, the sequence is small enough to follow by hand, but the same reasoning applies when a variable controls a longer run of worksheet operations. One step at a time.

Making the example safer to extend

The example leaves the variable’s declaration out, which keeps attention on how its value changes. In working VBA, it’s generally clearer to declare a variable and choose a type that matches its job. A row number is typically represented by an integer type, such as Long, and a declaration makes the variable’s purpose easier to see when someone returns to the procedure later.

There’s another detail in the example: Cells(i, 1) doesn’t name a worksheet. In that form, VBA uses the active sheet, so the result depends on which sheet is active when the procedure runs. If you're working in a workbook with several sheets, that can make the macro less predictable. Naming the intended worksheet is a common way to make the destination explicit.

This is more significant than it looks. A variable can make the row dynamic, but it doesn’t by itself make the whole operation safe or self-explanatory. The worksheet reference, the starting value, and the point at which the variable changes all affect what the macro does.

Why this matters for worksheet macros

A fixed reference such as Range("A2") always points to the same address. A reference such as Cells(i, 1) depends on the value held in i. As the procedure changes that value, the target row changes too. That’s the central idea: a variable lets a macro base an operation on a value that can change during execution.

This simple example still writes the assignment three times. That repetition is manageable here, but becomes awkward when the same operation needs to run across many rows. Repeating statements also means that a change to the operation may need to be made in several places. A loop can repeat the assignment while updating the row value, which is the natural next step after understanding how the variable works.

Here's the thing: a variable explains how a macro can move from one row to another, while a loop explains how to repeat that movement without writing a separate assignment for every row (and this is the part most people overlook). If you're working in this space, getting comfortable with the variable first makes loop logic easier to read: the loop controls repetition, while the variable can track the current row. The next lesson covers loops in VBA.

For practice, the online Excel exercises provide further exercises. Trying a small example and checking which cell changes after each increment can make the sequence easier to follow. Then you can adapt the idea to a different column or starting row and see how the variable controls the destination.

Source: Frédéric LE GUEN · excel-tutorial.com

Same flag