Professional Documents
Culture Documents
VBA Code
BY PAUL KELLY·2 COMMENTS
“The battlefield is a scene of constant chaos. The winner will be the one who controls that
chaos” – Napoleon Bonaparte
Ever feel like all the VBA examples are way simpler than the code you face in real life?
A post with simple code is good for explaining a topic clearly. It can make things much easier
to understand.
But the real world is more complex. The code you meet can often look like it was fed through
the Enigma machine first.
In this post, I’m going to do something different. I’m going to take code from a real world
application and
If you understand the ideas presented here it will dramatically improve you skills when you
deal with real world VBA code.
Contents [hide]
1 Quick Notes
2 The Code
3 Breaking the Code into Simple Parts
o 3.1 The Left of the Equals
o 3.2 How the For Loop works
o 3.3 The Second For Loop
3.3.1 Running the Code
3.3.2 A Quick Look at the Values
o 3.4 Finding the second cell
o 3.5 Finding the third cell
4 Oh No, My Brain is Bursting!
5 Giving the Code a Makeover
o 5.1 Step 1: Remove the Calcs
o 5.2 Step 2: Create Useful Variables
o 5.3 Step 3: Rewrite the For Loops
o 5.4 Step 4: Rewrite the Cell Assignment Code
o 5.5 Step 5: Rewrite the Cell Values Code
o 5.6 Final Code
6 Version 2 – Use the For Each Loop
7 Conclusion
8 What’s Next?
9 Get the Free eBook
Quick Notes
1. The underscore character “_” is used to break up long lines of code in VBA. The code
behaves exactly the same as if it was on one line. It’s just easier to read.
2. To view the Immediate Window select View->Immediate Window from the menu or
press Ctrl + G.
3. If you are new to VBA you may want to get familiar with VBA Cells and Ranges first.
4. For more info on the Loops used here, go to For Loops and For Each Loops.
The Code
Imagine you have been given the following piece of code to change
Sub Update()
Dim collect As Long, inptr As Long
collect = 3
inptr = 11
For x = 1 To 4
For j = 1 To 5
Next j
Next x
End Sub
Starting Data
If you are familiar with VBA you will have seen code like this a lot. If you are new to VBA then
this code can seem daunting.
What do the numbers mean? What are the calculations for? What does it all mean?
These are some of the questions you may have.
I’m going to answer these questions. Then I’m going to break the code down in the simplest
terms possible. I’m going to explain each part in plain English.
The code on the right of the equals is adding the value of two cells. The cell to the left of the
equals receives this value.
This is what the code is doing in simple terms
Cell1 = Cell2 + Cell3
Let’s look at Cell1 first and see which cell or cells the code is referring to
The Cells property allows us to read from or write to a cell on a worksheet. Remember
that Cellstakes Row and Column as arguments. So for example
Cells(1,1) refers to the cell A1
Cells(5,2) refers to the cell B5
Cells(2,1) refers to the cell A2
This code writes a value to a cell. We can see the row and column of the cell is
Row: x + collect
Column: 4 + j
We know that the value of collect is set to 3 at the start of the Sub
collect = 3
For x = 1 To 4
Next x
What a For loop does is to repeat a line of code a given number of times. In the above code it
will repeat the lines between For and Next four times. That is the values from 1 to 4.
The first time the loop runs x will have the value 1. The second time, x will be 2, third time 3
and the fourth time 4. Let’s look at the code
For x = 1 To 4
Next x
As the code runs through the loop the values change as follows
x collect Row
1 3 4
2 3 5
3 3 6
4 3 7
For x = 1 To 4
For j = 1 To 5
Next j
Next x
If you are new to programming a loop within a loop may seem tricky. The following will show
the values as the code runs
1 3 4 4 1 5
1 3 4 4 2 6
1 3 4 4 3 7
1 3 4 4 4 8
2 3 5 4 1 5
2 3 5 4 2 6
2 3 5 4 3 7
x collect x + collect 4 j 4+j
2 3 5 4 4 8
3 ...
Cells(x + collect, 4 + j - 1)
Cells(x + collect, 4 + j) =
The difference is that it refers to the cell one column to the left.
So the code
Cells(x + collect, 4 + j).Value = Cells(x + collect, 4 + j - 1)
is saying
E4 = D4
F4 = E4
G4 = F4 and so on
Cells(inptr, j).Value
The variable inptr is set to 11 at the start of the Sub. It never changes. So this cell is always in
row 11.
The column we use here takes the value j.
So when x = 1 and j=1 our code
Cells(x + collect, 4 + j) = _
gives
which is doing:
E4 = D4 + A11
So
x=1 and j=1 gives E4 = D4 + A11
x=1 and j=2 gives F4 = E4 + B11
x=1 and j=3 gives G4 = F4 + C11
x=1 and j=4 gives H4 = G4 + D11
x=1 and j=5 gives I4 = H4 + E11
x=2 and j=1 gives E5 = D5 + A11
x=2 and j=2 gives F5 = E5 + B11
and so on.
The following screenshots shows the first two values being assigned
E4(Red)= D4(Blue) + A11(Blue)
Sub Update()
collect = 3
inptr = 11
For x = 1 To 4
For j = 1 To 5
Next j
Next x
End Sub
For x = 1 To 4
Cells(x, 4 + j).Value =
Then we don’t need to calculate the row each time. x is already the correct row.
colStart = 5
colEnd = 9
rowStart = 4
rowEnd = 7
Next i
' Original
Cells(i, j) = Cells(i, j - 1)
So you can see the code is much more readable. We only have one calculation for a cell and that
is subtracting 1 from j.
rowValues = 11
The value in A11 is added to E4, B11 is added to F4 and so on. So for the first value we add
from column 1, the second value from column 2 etc.
Let’s create a counter for this column
Dim colCnt As Long
We place this counter before the second loop. This means it starts at one each time the For j loop
runs. We then a line to the loop that adds one to it each time so it moves on one column.
colCnt = 1
colCnt = colCnt + 1
Next j
Final Code
The final code including comments looks like this
Sub UpdateNew()
rowStart = 4
rowEnd = 7
colStart = 5
colEnd = 9
colCnt = 1
colCnt = colCnt + 1
Next j
Next i
End Sub
You can see this code is much more readable. The variable names give a clear indication of what
the values are.
There are no complicated calculations. This makes code easier to read or change. It is also less
likely to have errors.
For completeness I’m going to show another way you could perform the same task.
Next rCell
rCell =
The first part of the calculation is the cell to the left. We can use the Offset property of range.
To get one cell to the left we use a minus value
Now we use the offset to get the value cell from the current one. The row is always 11 i.e.
rowValues. The column is 4 less than the current cell column
Sub UpdateForEach()
Next rCell
End Sub
You can see we have broken down the calculation into two lines. This makes the code easier to
read and understand.
We also only have one For loop which means we have not complex calculations to figure out.
Conclusion
If you have read this far then congratulations! This was not a simple topic. If you can
grasp the ideas then you are well on the way to becoming proficient at VBA.
So what we did here was as follows
If you come face to face with complex code you can approach it like this. Break it down into
simple parts. Try to understand what each part is doing.