Professional Documents
Culture Documents
Excel VBA Speed and Efficiency SOA
Excel VBA Speed and Efficiency SOA
soa.org
by Kevin Roper
The first five rules generally have the largest impact on macro
performance. Rules six through 11 have a marginal impact. Please
note that my estimates of time savings below may vary
significantly for your specific application. The analysis used Excel
2007.
This rule is well known, but it is the most important rule. When a
new value is entered into a worksheet cell, Excel will recalculate all
the cells that refer to it. If the macro is writing values into the
worksheet, VBA will need to wait until the worksheet is done
recalculating each entry before it can resume. The impact of
1 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
Application.Calculation = xlCalculationManual
Calculate
Worksheets("sheet1").Calculate
Range("A1:C5").Calculate
Application.Calculation = xlCalculationAutomatic.
Application.ScreenUpdating = FALSE
At the end of the macro use the following command to turn screen
updates back on.
Application.ScreenUpdating = TRUE
2 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
In this example, the macro will need to grab the named Range
"issue_age" from the worksheet repeatedly. This is a common
mistake. VBA is much faster when it doesn't need to stop and
interact with the worksheet.
Issue_Age = Range("Issue_Age")
For Duration = 1 to 100
Attained_Age = Issue_Age + Duration
Next Duration
3 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
Likewise, here are examples of writing the array back into the
worksheet. All are roughly 40 times faster than writing each of the
2,600 cells individually within a loop.
Method #1
Worksheets("Sheet1").Range("A1:Z100").value = myArray
Method #2
With Worksheets("Sheet1")
.Range("A1:Z100").Value = myArray
End With
Method #3
4 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
VBA. This is clearly not the case. For example, most VBA users
are probably aware that VBA does not have a Max() or Min()
function. Excel does have these functions. It is common to use the
following code that uses the Excel version of Max():
5 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
6 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
If bMale Then
(insert code here)...
ElseIf bFemale Then
(insert code here)...
ElseIf bUnisex Then
(insert code here)...
End If
Avoid this:
Worksheets("sheet1").Select
Amount1 = Cells(1, 1)
Do this instead:
Amount1 = Worksheets("sheet1").Cells(1,1)
7 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
VBA can process math faster than it can display the StatusBar.
Writing to the StatusBar is another example of traffic between VBA
and Excel. The following example writes one out of every 100
scenarios to the StatusBar. It is about 90 times faster than writing
every scenario to the StatusBar.
Interest_rate = .05
For i = 1 To 600
fund(i) = fund(i-1) * (1+interest_rate)^(1/12)
Next i
8 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
Interest_factor = (1+interest_rate)^(1/12)
For i = 1 to 600
fund(i) = fund(i-1) * interest_factor
Next i
Range("A1:Z100").value = Range("A101:Z200").value
Final Thoughts
9 of 10 31-Jul-2023, 7:36 PM
Excel VBA Speed And Efficiency | SOA about:reader?url=https%3A%2F%2Fwww.soa.org%2Fnews-and-public...
I also want to point out and thank several excellent articles on the
internet that address this topic:
Kevin Roper, FSA, MAAA is actuary with AEGON USA Inc. He can
be contacted at kroper@aegonusa.com
10 of 10 31-Jul-2023, 7:36 PM