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.
Related
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
I tried all the different blocks of codes below but when I close the Excel, it still prompts whether to save or not.
I have the code in the Workbook BeforeClose event.
I am using excel 2019 and Windows 11
I tried this,
Application.EnableEvents = False
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
this,
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
and this,
Application.EnableEvents = False
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Am I missing something?
Application.Calculation = xlCalculationAutomatic is a workbook change. Hence the previous saved state is no longer valid. You can confirm this easily enough my doing the same thing manually. i.e. toggle calculation state after a save and attempt to close the book.
Interestingly enough, it you save one book with Auto calc on and close it. Then save another with Auto calc off and close it. The calc state will be reflected by whichever you next open. That is to say, it's saved in the workbook (not the application). Excel resolves conflicts by adopting the current state to any subsequent books opened.
This isn't new by the way. Been like that as long as I can remember. Which, unfortunately, is too long :)
I have a workbook that is designed to essentially operate as a standalone application. All sheets are protected, the individual excel tabs are hidden, and the top excel ribbon is hidden as well for the entire workbook.
The following VBA code performs the above procedures and is applied to every sheet within the workbook.
Sub masque()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayFullScreen = True
Application.DisplayStatusBar = Not Application.DisplayStatusBar
Application.WindowState = xlMaximized
ActiveWindow.WindowState = xlMaximized
Application.DisplayFormulaBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I want to use the following code as a button to override the prior code in order for an individual to easily switch between an "editor" mode and "user mode"
Sub masteredit()
Application.ScreenUpdating = False
ActiveWindow.View = xlNormalView
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayStatusBar = False
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True
ActiveWindow.DisplayWorkbookTabs = True
Application.DisplayFullScreen = False
Application.DisplayFormulaBar = True
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.ScreenUpdating = True
What is a good way to accomplish this?
well, you could have a predermined type of user, for example, by default the app will be for user_mode. So you use a special sub called auto_open(), that will be run each time you open the workbook. So we have
sub auto_open()
call masque
end sub
And you can add buttons whenever you want to change between editor mode and user mode. You can put sub auto_open() in any module you want, because it is detected as special by Vba
Solved the issue.
I forgot I had the following code on every sheet in the workbook.
Sub Worksheet_Open()
Call masque
End Sub
Sub Worksheet_Activate()
Application.ScreenUpdating = False
Call masque
Application.ScreenUpdating = True
End Sub
Therefore, any time I tried to navigate to a different sheet, the masque would be applied.
To solve my issue, I removed the prior code from every page. I then added a checkbox on my "Home" page that used the following code.
Sub Worksheet_Open()
Call masque
End Sub
Sub Worksheet_Activate()
Application.ScreenUpdating = False
Call masque
Application.ScreenUpdating = True
End Sub
This way, when you open up the workbook the masque is automatically applied. However when you click the checkbox you enter editor mode and the masteredit sub is applied. And when it is unchecked, the masque is once again applied.
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
My macro updates a large spreadsheet with numbers, but it runs very slowly as excel is rendering the result as it computes it. How do I stop excel from rendering the output until the macro is complete?
I use both of the proposed solutions:
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
...
...
...
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.ScreenUpdating = False
And of course set it back to True again when you've finished, even if an error is raised.
Example:
Public Sub MyMacro
On Error GoTo ErrHandler
Application.ScreenUpdating = False
... do my stuff that might raise an error
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
Application.ScreenUpdating = True
... Do something with the error, e.g. MsgBox
End Sub
I also prefer to use both of the proposed solutions,
but also keeping the users previous calculation mode.
For this particular application this might be no biggie,
but it's usually best practice to let the users have their settings restored after your procedure is finished:
Application.ScreenUpdating = False
PreviousCalcMode = Application.Calculation
Application.Calculation = xlCalculationManual
...
...
...
Application.Calculation = PreviousCalcMode
Application.ScreenUpdating = True
NB. It would also be worth your time to insert some error handling that turns on Application.ScreenUpdating should an error occur in your otherwise brilliant code ;)
If memory serves me right, Excel will not show any errormessages etc when ScreenUpdating = false.
Something like this:
Sub DoSomeThing
On Error Goto DisplayError
Application.ScreenUpdating = False
PreviousCalcMode = Application.Calculation
Application.Calculation = xlCalculationManual
...
...
...
Application.Calculation = PreviousCalcMode
Application.ScreenUpdating = True
Exit Sub
DisplayError:
Application.Calculation = PreviousCalcMode
Application.ScreenUpdating = True
MsgBox Err.Description
End 'This stops execution of macro, in some macros this might not be what you want'
'(i.e you might want to close files etc)'
End Sub
Building upon Joe and SeeR (this uses old syntax so it's compatible with VBA of Office 2000):
On Error Goto AfterCalculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
...
AfterCalculation:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
You can turn off automatic calculation in the options dialog, it sets it so that it only calculates when you press F9.