Link to home
Start Free TrialLog in
Avatar of Angus
AngusFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel VB Macro - Switch on & Off Calculations

Hi,
I have written a VB macro that is very lengthy and executes a number of calculations.

If the macro is run and the Excel Option "Calculation Options" is set to "Automatic" - the macro takes ages.  So it needs to be set to "Manual".

Is there any way of doing this directly in the VB script.  This prevents idiots running it with it on.

I am using Excel 2007

Cheers
A.
ASKER CERTIFIED SOLUTION
Avatar of Dave
Dave
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dambuster99
dambuster99

h there,
try

Application.Calculation = xlCalculationManual

cheers
The other consideration if you have event handlers is to set application.EnableEvents before data is changed and back to true afterwards ... or at the start of your routine and the end if applicable, (as is oftern the case).

Chris
The code attached shows how you can switch calculation into manual, run some code, then put it back to how it was.

Hope this helps =)
Sub GoToManual()
 
Dim xlCalc As XlCalculation
 
    xlCalc = Application.Calculation
 
    Application.Calculation = xlCalculationManual
 
    On Error GoTo CalcBack
 
    'YOUR CODE
 
    Application.Calculation = xlCalc
    
    Exit Sub
 
CalcBack:
 
Application.Calculation = xlCalc
 
End Sub

Open in new window

oops ... application.EnableEvents = false at the start and application.EnableEvents = true at the end.

Chris