VBA Performance Code

animas

Active Member
Joined
Sep 28, 2009
Messages
396
I found the following code for using in projects to increase VBA performance.
Code:
[FONT=Courier]Option Explicit
Option Private Module


Private mlCalcStatus As Long
Private mbInSpeed As Boolean


Public Sub speed()
   On Error Resume Next
   If Not mbInSpeed Then
      Application.ScreenUpdating = False
      Application.DisplayAlerts = False
      Application.EnableEvents = False
      mlCalcStatus = Application.Calculation
      Application.Calculation = xlCalculationManual
      mbInSpeed = True
   Else
      'we are already in speed - don't do the settings again
   End If
End Sub


Public Sub unspeed()
   On Error Resume Next
   Application.ScreenUpdating = True
   Application.DisplayAlerts = True
   Application.EnableEvents = True
   If mbInSpeed Then
      Application.Calculation = mlCalcStatus
   Else
      'this shouldn't be happening - put calc to auto - safest mode
      Application.Calculation = xlCalculationAutomatic
   End If
   mbInSpeed = False
End Sub[/FONT]

What do you use for your projects?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Turn screen updating off, yes, this can make a huge difference.

Turning off alerts shouldn't speed up your code by much, if anything. Beware of turning off alerts if they're likely to be telling you something useful.

Setting calculation to manual can help, but code may be relying on calculation to be taking place during program execution, so in that case it's a definite "no".
 
Upvote 0
Consider disabling events before opening workbooks programatically in case they have Workbook_Open macros which may disrupt your processing.

(Disabling events won't stop autoexec macros running when you open an Access database programatically.)

If you disable events, event-driven macros like Worksheet_Change and Worksheet_Calculate won't run, so if they play a vital part in the operation of your workbook you may get erroneous results.
 
Upvote 0

Forum statistics

Threads
1,224,559
Messages
6,179,513
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top