Solved

Excel VB Macro - Switch on & Off Calculations

Posted on 2008-09-29
5
685 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

12 Experts available now in Live!

Get 1:1 Help Now