9 Quick Tips To Improve Your VBA Macro Performance - Microsoft Community Hub
9 Quick Tips To Improve Your VBA Macro Performance - Microsoft Community Hub
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 1/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
Option Explicit
Dim lCalcSave As Long
Dim bScreenUpdate As Boolean
Sub SwitchOff(bSwitchOff As Boolean)
Dim ws As Worksheet
With Application
If bSwitchOff Then
' OFF
lCalcSave = .Calculation
bScreenUpdate = .ScreenUpdating
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableAnimations = False
'
' switch off display pagebreaks for all worksheets
'
For Each ws In ActiveWorkbook.Worksheets
ws.DisplayPageBreaks = False
Next ws
Else
' ON
If .Calculation <> lCalcSave And lCalcSave <> 0 Then .Calculation = lCalcSav
.ScreenUpdating = bScreenUpdate
.EnableAnimations = True
End If
End With
End Sub
Sub Main()
SwitchOff(True) ‘ turn off these features
MyFunction() ‘ do your processing here
SwitchOff(False) ‘ turn these features back on
End Sub
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 2/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
HIVE: HKEY_CURRENT_USER
Key Path: Software\Microsoft\Office\16.0\Common\Graphics
Key Name: DisableAnimations
Value type: REG_DWORD
Value data: 0x00000001 (1)
Warning: Using Registry Editor incorrectly can cause serious, system-wide problems
that may require you to re-install Windows to correct them. Microsoft cannot
guarantee that any problems resulting from the use of Registry Editor can be solved.
Use this tool at your own risk.
Before
Sheets("Order Details").Select
Columns("AC:AH").Select
Selection.ClearContents
After
Sheets("Order Details").Columns("AC:AH").ClearContents
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 3/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
When working with objects, use the With statement to reduce the number of times
object properties are read. The following example shows the code before and after
making the change to use the With statement.
Before
Range("A1").Value = “Hello”
Range("A1").Font.Name = “Calibri”
Range("A1").Font.Bold = True
Range("A1").HorizontalAlignment = xlCenter
After
With Range("A1")
.Value2 = “Hello”
.HorizontalAlignment = xlCenter
With .Font
.Name = “Calibri”
.Bold = True
End With
End With
This means that you should try to reduce the number of times you pass data
between VBA and Excel. This is where ranges are useful. Instead of reading and
writing to each cell individually in a loop, read the entire range into an array at the
start, loop through the array, and then write the entire array back at the end. The
following example code shows how a range can be used to read and write the
values once, instead of reading each cell individually.
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 4/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
.Text is commonly used to retrieve the value of a cell – it returns the formatted value
of a cell. Getting the formatting of a cell is more complex than just retrieving a value,
and makes .Text quite slow.
.Value is an improvement over .Text, as this mostly gets the value from the cell,
without formatting. However for cells formatted as a date or currency, .Value will
return a VBA date or VBA currency (which may truncate decimal places).
.Value2 gives the underlying value of the cell. As it involves no formatting, .Value2 is
faster than .Value. .Value2 is faster than .Value when processing numbers (there is
no significant difference with text), and is much faster using a variant array.
For a more detailed explanation, please see Charles William’s blog post, “TEXT vs
VALUE vs VALUE2”: https://fastexcel.wordpress.com/2011/11/30/text-vs-value-vs-
value2-slow-text-and-how-to-avoid-it/
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 5/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
When you use the Macro Recorder to record operations that use copy and paste,
the code will use the copy and paste methods by default. However, within VBA code,
it is much faster to bypass the clipboard and use internal operations instead. By
default, copying will copy everything, including formulas, values and formatting. You
can make copying faster by only copying values or formulas, without the formatting.
The following example shows the code before and after making the change to
bypass the clipboard.
Before
Range("A1").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
After
If you still find that a macro takes longer than expected to execute many individual
copy and paste operations, you may want to apply the following hot
fix: https://support.microsoft.com/en-in/help/2817672/macro-takes-longer-than-
expected-to-execute-many-in...
This can be set by typing: Option Explicit at the top of each module in your project or
by checking the "Require Variable Declaration" option under Tools -> Options in the
VBA editor.
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 6/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
Conclusion
We hope that this has helped highlight some of the ways that you can make your
macros run faster. We’re sure that we haven’t covered everything, so please
comment below with any other tips or tricks to improve the performance of your
macros in Excel.
Further reading
https://blogs.office.com/en-us/2009/03/12/excel-vba-performance-coding-best-
practices/
http://datapigtechnologies.com/blog/index.php/ten-things-you-can-do-to-speed-up-
your-excel-vba-code/
https://www.ozgrid.com/VBA/SpeedingUpVBACode.htm
FAQs:
After using the new macro animations stopped working/it’s stuck on manual
calculation.
It is possible the code disables the various settings, but the macro crashes before
re-enabling these settings. To fix this, you will need to run the code to enable these
settings again.
Other resources
https://www.thespreadsheetguru.com/blog/2015/2/25/best-way-to-improve-vba-
macro-performance-and-prev... - The Best Way To Improve VBA Macro Performance
And Prevent Slow Code Execution
https://www.ozgrid.com/VBA/SpeedingUpVBACode.htm - Optimize Slow VBA Code.
Speeding Up Slow Excel VBA Code
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 7/8
4/27/23, 2:07 PM 9 quick tips to improve your VBA macro performance - Microsoft Community Hub
popular utility for managing defined names. For more information about Excel
calculation performance and methods, memory usage, and VBA user-defined
functions, visit the Decision Models Web site.
Jan Karel Pieterse is a long time Excel MVP who develops custom solutions
focused on Microsoft Office, with deep expertise in Excel and VBA. He runs the
website JKP Application Development Services site, where you can find an
interesting collection of articles, training events, and utilities. For a good overview of
topics, see this list of in-depth articles. Jan develops some cool and useful utilities
for Excel, including NameManager, RefTreeAnalyser, and Flexfind. You can find a
full list on the downloads page.
https://techcommunity.microsoft.com/t5/excel/9-quick-tips-to-improve-your-vba-macro-performance/m-p/173687 8/8