Solved

Excel VB Macro - Switch on & Off Calculations

Posted on 2008-09-29
5
688 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:amacfarl
5 Comments
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 22595950
Application.Calculation = xlCalculationManual
'at end
Application.Calculation = xlCalculationAutomatic

Cheers

Dave
0
 
LVL 6

Expert Comment

by:dambuster99
ID: 22595958
h there,
try

Application.Calculation = xlCalculationManual

cheers
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 22595982
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
0
 
LVL 2

Expert Comment

by:sal-ee
ID: 22595986
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

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 22595993
oops ... application.EnableEvents = false at the start and application.EnableEvents = true at the end.

Chris
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now