Application.Calculate Method doesn't work - excel

I have a complex Excel with lot of tables and formulas.
I stop and resume calculations via VBA code:
Turn OFF calculations:
ActiveSheet.EnableCalculation = False
Application.Calculation = xlCalculationManual
Turn ON calculations:
ActiveSheet.EnableCalculation = True
Application.Calculation = xlCalculationAutomatic
Application.Calculate 'This line calculates all open books and is not strictly necessary.
But still the cells are not calculated.
They are only calculated when I edit each cell with double click and hit Enter (one by one!).
The code doesn't give me any errors and works perfectly on simple books.

The problem is "EnableCalculation" method.
If I remove it, then the book works perfect. So simply toggle ON/OFF calculations with xlAutomatic/xlManual, like this:
Turn OFF calculations:
Application.Calculation = xlCalculationManual
Turn ON calculations:
Application.Calculation = xlCalculationAutomatic

Related

How To Change Excel Calculation to Manual Before Workbook Does Calculation

I am developing an add in for excel using vba. Upon opening the spreadsheet, all the values are recalculated and this slows down the opening. I've tried creating a Workbook_Open event that changes the application calculation from automatic to manual, but this event handler is executed after the calculations are done. I also tried setting the calculation to manual before the spreadsheet closes, so that upon opening it next time it will be faster. My problem with this though is that I feel it is invasive to the client.
Ideally what I would like to do is:
When the spreadsheet opens get the user's current calculation setting and save it
Change the calculation setting to manual so that the spreadsheet can open quickly
Change the calculation setting back to the user's original setting
How can I go about doing this?
I created 2 subs that I call when I need to do this, Updates_Off and Updates_On. You will need to declare the variables CalcMode, IterationMode, and Iterations as public in your calling sub and this will only work if you turn updates back on before you exit. If you can't do that, you will need to have some helper cells that store the values.
I have the if CalcMode=xlCalculationManual statements to warn me during development that I'm starting or ending in manual mode and I uncomment them when needed.
Public Sub Updates_Off()
' Turn off Screen updating
Application.ScreenUpdating = False
' Check what calculation mode is in effect and set current to manual
CalcMode = Application.Calculation
' If CalcMode = xlCalculationManual Then _
' MsgBox "Starting mode is manual"
IterationMode = Application.Iteration
Iterations = Application.MaxIterations
Application.Calculation = xlCalculationManual
End Sub
Public Sub Updates_On()
' Turn on Screen updating
Application.ScreenUpdating = True
' Reset Calculation mode
Application.Calculation = CalcMode
Application.Iteration = IterationMode
Application.MaxIterations = Iterations
' If CalcMode = xlCalculationManual Then _
' MsgBox "Reset to manual mode"
End Sub

Application.Calculation after subroutine

All my UDF functions have
Application.Volatile
When I run a subroutine and end with
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.Calculate
Is the Application.Calculate not needed when Application.Calculation = xlCalculationAutomatic?
By default UDFs in Excel are non-volatile. See here: Volatile functions
They are only recalculated when any of the function's arguments change.
You are forcing a recalculation with the .Calculate thereby making it volatile.

Deleting column using vba takes a huge amount of time

Newbie here.
I'm trying to delete a column using vba and it is taking a HUGE amount of time.
Sub Add_Delete()
Application.ScreenUpdating = False
Workbooks("f.xlsx").Sheets("g").Columns("FD:FD").EntireColumn.Delete
End Sub
There are about 20,000 rows with a total of amount 230 columns. There are no calculations being performed. Just data
What am I doing wrong? Thanks in advance!
If it is slow manually also turn off calculation before running it, then turn it back on.
Sub Add_Delete()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Workbooks("f.xlsx").Sheets("g").Columns("FD:FD").EntireColumn.Delete
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
add screen updating back in also... ;-)

How can I enable and disable expensive calculations in an Excel spreadsheet?

I use huge excel worksheets and they take forever to load because of cell calculations using macros and other formulas (over 5 minutes even if I have a good computer) . I was wondering if there was a way to save the excel files with the current cell values instead of calculating the cells each time I open the file.
What I am looking for is like a switch that would turn the calculations on and off so that when I need to use them I could set them to on, and when I am done, I could switch it to off and the cells would keep their current values.
Maybe I could create a macro that would do something like that, or maybe I am just dreaming and there is no other way around, so I should just sit and wait.
We have a similar problem.
This is what we use:
Function TurnOfCalcs()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.DisplayAlerts = False
End Function
We turn off calculations, screenupdating, alerts and events while the initial data is loading and updating.
Once the streaming data from the sheet has finished we turn updates back on like so:
Function TurnOnCalcs()
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.DisplayAlerts = True
End Function
You still have the udpate time but this means you don't' do updates after each single cell change, which should dramatically speed up your file loading times.
You can set the calculation options for Excel both via the GUI and via VBA: Application.Calculation = xlManual
Here is some more info... http://www.decisionmodels.com/calcsecretse.htm
Use:
Private Sub Workbook_Open()
Module1.TurnOff
End Sub
Then have a button than is assigned this macro
Sub Button1_Click()
If Application.EnableEvents = True Then
TurnOff
Else
TurnOn
End If
End Sub
Then have this in a module:
Public Sub TurnOn()
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Public Sub TurnOff()
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub

Is there a way to prevent Excel from automatically setting Application.ScreenUpdating to True

I have a COM (C++) API that listens for data updates from a server and writes these updates to a sheet. These updates are handled in VBA code and can arrive multiple times a second. In order to write these updates to the sheet in the most efficient manner, I use the following premise:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
<UPDATE CODE>
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
In fact, I schedule a procedure to do this regular intervals where ScreenUpdating = False for about 20 seconds, then it is set to true so the data can update and then I set it to false again. i have found that this is a better option than setting ScreenUpdates + Calculation explicitly simply because of the highfrequency of the updates I receive.
The Problem:
I have read here that excel sets ScreenUpdating = True at the end of each method that disables it which is not what I need.
The Question:
Is there some way to force Excel to not automatically enable ScreenUpdating?
See if these help with your APIs
http://msdn.microsoft.com/en-us/library/ff818516%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ee461765%28v=vs.85%29.aspx
I just searched google for "list of apis microsoft"

Resources