Skip to the stories
Excel & Power BI

Excel VBA Programs: How Instructions, Range and Cells Work

VBA runs instructions in order and uses Excel objects such as Range and Cells. A fixed cell example shows how assignment works and where hard-coded references fall short.

Excel VBA Programs: How Instructions, Range and Cells Work

A VBA program gives Excel instructions to carry out in sequence. For administrators and Excel power users, the key distinction is that VBA combines the Visual Basic language with objects that belong to the host application. A short procedure can write values to cells and calculate results, but fixed cell addresses limit where that code can be reused.

VBA instructions are grouped in a procedure, such as a Sub procedure. The computer executes its statements in order, from the first line to the last. That order matters: a statement can use a value written by an earlier statement, as in a macro that first puts a number in one cell and then reads it to calculate another.

Assignment sends a result to the left side

In an assignment statement, VBA evaluates the expression on the right side of the equals sign and stores the result in the item on the left. The sign is therefore performing an assignment, rather than asking whether two mathematical expressions are equal.

Destination = Expression

For example, in Range("A1") = 10, the expression on the right is the number 10, and the destination on the left is cell A1. In Range("B1") = Range("A1") * 2, VBA reads the value in A1, multiplies it by two, and writes the result to B1. Since the first statement runs before the second, the calculation has a value available to read.

image excel exercises

This pattern—calculate a value, then assign it to a destination—is common across programming languages. Once you recognize it, VBA statements become easier to read: identify what is being calculated on the right, then look at where the result will go on the left.

Excel supplies the objects VBA can control

The name Visual Basic for Applications describes both parts of the environment: Visual Basic is the language, and Applications refers to the programs it can automate. In Excel VBA, terms such as Range, Cells, Worksheets, Sheets and Workbooks refer to Excel objects. These let code work with cells, groups of cells, sheets and workbook files.

The available objects depend on the application. Word VBA, for example, includes concepts such as documents and paragraphs. That application context is why a VBA macro can interact with parts of Excel directly: it uses the objects Excel provides, along with the language’s statements and calculations.

image excel quizzes

Two ways to address cells

This procedure writes 10 to A1, then doubles that value and stores the result in B1:

Sub FirstCalculation()
    Range("A1") = 10
    Range("B1") = Range("A1") * 2
End Sub

The same two actions can be expressed with Cells:

Sub FirstCalculation()
    Cells(1, 1) = 10
    Cells(1, 2) = Cells(1, 1) * 2
End Sub

In both versions, the result is 10 in A1 and 20 in B1. The two forms use different address formats. Range("B1") writes the column letter first and the row number second. Cells(1, 2) takes the row number first and the column number second, so it means row 1, column 2: B1.

VBA procedure FirstCalculation in a module, with 10 in cell A1 and 20 in B1

For a single cell, either notation can identify the destination. The main point when reading or editing a procedure is to keep the argument order straight. Mixing up the row and column in a Cells reference can send a value to a different cell than intended.

Fixed addresses make the example hard to reuse

The procedure is a clear demonstration, but its cell references are fixed in the code. It always reads A1 and writes to B1. If the input moves to a different location, those references must be changed. If the same operation needs to be applied to 100 numbers, repeating the two statements for each input would create a long procedure with many fixed addresses.

That limitation is a useful signal when designing a macro: a procedure that works for one example may still need a more flexible way to represent its inputs and destinations. VBA variables provide that flexibility by letting code refer to values that can change, rather than embedding every value or location directly in a statement. The next topic in this learning sequence is what a variable is in VBA.

What to take into your next macro

When reviewing a small VBA procedure, follow its statements from top to bottom. For each assignment, check the expression on the right, the destination on the left and whether an earlier statement supplies the value being read. Then check whether cell references are fixed to one location or whether the procedure needs to handle different inputs. Those checks help explain both what a macro currently does and how far its logic can be reused.

For practice with Excel, online Excel exercises are available. For VBA specifically, the next useful step is to learn how variables can make a procedure work with changing values and locations.

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

Same flag