Drawbacks with Application.Calculation?

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
Are there any risks or drawbacks with using Application.Calculation in my scripts?

VBA Code:
Application.Calculation = xlCalculationManual
[My code]
Application.Calculation = xlCalculationAutomatic

I mean, when it's turned on again, all cell calculation are updated. Right?
Or/and are there any particular situation were I should use Application.Calculation?
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
By default Excel has calculations set to Automatic

At times when Excel slows down due to excessive calculations, some people (including me) chose to turn calculations to Manual

If your file is not giving any trouble with Auto Calculations - Prefer keeping it that way (Automatic) only. It's better and safer practice to follow.

Hope it helps in some useful way.

Edited and added later 👇 -
Just to add at times some codes might need to turn Calculation to manual to fasten up the whole process and later turn it on to automatic. But it's very subjective
 
Upvote 0
Except in the most trivial of cases I always turn autocalculation off in VBA prior to executing the rest of the code, and turn it back on at the end. In addition I turn off screen updating and add a message to the user. It makes code execution go very much faster.

VBA Code:
            ' Disable screen updating and recalculation, display message
            Application.ScreenUpdating = False
            Application.Calculation = xlCalculationManual
            Load frmWaitMessage
            With frmWaitMessage
                .Label1.Caption = "Adding data to database."
                .Caption = "Please wait..."
                .Show vbModeless
                DoEvents
            End With
            '
            ' Code
            '
            '
            ' Unload message, Enable screen updating and recalculation
            ' 
            Unload frmWaitMessage
            Application.ScreenUpdating = True
            Application.Calculation = xlCalculationAutomatic
            '
            ' Completion message to user
            '
            MsgBox "Finished"
 
Upvote 0
Are there any risks or drawbacks with using Application.Calculation in my scripts?
If your code changes some values and you need to have an updated formula result to use further in your code - then you must make sure that the formula results you get are correctly updated.
If you code/project relies on the Calculate event for some reason - it will not fire automatically.
If your code does not rely on formula results, calculated during runtime or the Calculate event, then it's OK to boost the code performance.
I use this bit of code in most of my projects:
VBA Code:
Public Sub appBoost(Optional boost As Boolean = True)
    With Application
        If boost Then
            .EnableEvents = False
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        Else
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Are there any risks or drawbacks with using Application.Calculation in my scripts?

VBA Code:
Application.Calculation = xlCalculationManual
[My code]
Application.Calculation = xlCalculationAutomatic

I mean, when it's turned on again, all cell calculation are updated. Right?
Or/and are there any particular situation were I should use Application.Calculation?
The biggest thing to watch/be careful for is if you do shut it off, be sure it gets turned back on again.
You will want to be wary of using any "Exit Sub" statements, or any other code that may exit the code before it gets to the line where you turn it back on again.
And be weary of any error situations that may make your code exit before getting to that line.

This is true not just for calculation, but for events. We have had many questions where people asking why their VBA code is not running automatically, and it is often because they are temporarily disabling events with:
VBA Code:
Application.EnableEvents = False
and an error stopped their code before they got to the line that turned it back on.

It is easy enough to turn back on, if you understand why it is not working, i.e. just run a little procedure like this:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub
 
Upvote 0
"If your code changes some values and you need to have an updated formula result to use further in your code..."
Yes, and with long and complex code which been developed over several years might be cumbersome be to verify that, nevertheless needs to be considered.

The biggest thing to watch/be careful for is if you do shut it off, be sure it gets turned back on again.
Yes, that I noticed the hard way. When a script halted for some reason. Maybe should implement a regular check...

It is easy enough to turn back on, if you understand why it is not working, i.e. just run a little procedure like this:
Yes, and I noticed now, by doing a test. That it will go back to "Application.Calculation = xlCalculationAutomatic" when I reopen the file.

Thanks everybody for your input.
 
Upvote 0
Yes, and with long and complex code which been developed over several years might be cumbersome be to verify that, nevertheless needs to be considered.


Yes, that I noticed the hard way. When a script halted for some reason. Maybe should implement a regular check...


Yes, and I noticed now, by doing a test. That it will go back to "Application.Calculation = xlCalculationAutomatic" when I reopen the file.

Thanks everybody for your input.
What I have seen some people do is to add error handling code to the procedures, and if an error is encountered, go to an Error Handling block of code at the bottom of the procedure that resets all those things before exiting. That would prevent the "mid process cold halt" you are talking about.
 
Upvote 0

Forum statistics

Threads
1,223,220
Messages
6,170,805
Members
452,353
Latest member
ledowning

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