Excel VBA Performance Coding Best Practices
Excel VBA Performance Coding Best Practices
Excel VBA Performance Coding Best Practices
Here's some sample code that shows how and what to shut off while your code runs.
Doing this should help improve the performance of your code:
'Get current state of various Excel settings; put this at the beginning of your code
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
calcState = Application.Calculation
eventsState = Application.EnableEvents
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'after your code runs, restore state; put this at the end of your code
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.Calculation = calcState
Application.EnableEvents = eventsState
Application.ScreenUpdating: This setting tells Excel to not redraw the screen while
False. The benefit here is that you probably don't need Excel using up resources trying
to draw the screen since it's changing faster than the user can perceive. Since it requires
lots of resources to draw the screen so frequently, just turn off drawing the screen until
the end of your code execution. Be sure to turn it back on right before your code ends.
Application.EnableEvents: This setting tells Excel to not fire events while False. While
looking into Excel VBA performance issues I learned that some desktop search tools
implement event listeners (probably to better track document contents as it changes).
You might not want Excel firing an event for every cell you're changing via code, and
turning off events will speed up your VBA code performance if there is a COM Add-In
listening in on Excel events. (Thanks to Doug Jenkins for pointing this out in my earlier
post).