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 :)
Related
I have a code where I am copying sheets from one excel to another one. There are names that have to be copied over. Whenever that happens, there is a pop up (see below) that I wish to disable. I have tried all of the below, and none seem to work. It seems to work on some computers and not on the others, so there might be something with excel settings.
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Const ProcName As String = "ListSheets"
Application.AutomationSecurity = msoAutomationSecurityLow
Dim IsSuccess As Boolean
Does anyone know how to fix this?
Maybe try Application.AskToUpdateLinks = False just before opening command lines
I have two workbooks, one is for work (A) and the other only has usage data (B). File B has more than 25000 records and when I open it using VBA there is a long delay in loading.
One option I thought of was to delete the sheets with the data that I don't need but should delete them without opening the B file.
The second option would be to copy the data from the sheet without opening file B.
Currently, I am using Set wbOrigen = Workbooks.Open (FileName: = xxxxxx) but I need to speed up the load.
Is it possible to do any of this?
As far as I know it is not possible to access a Workbook's data without opening it. But from my experience this is not a problem (as this is very fast) as long as you remember to close it in the end.
The "standard" trick to improve performance is to disable ScreenUpdating and Events like such:
Application.ScreenUpdating = False
Application.EnableEvents = False
' Open Workbook, Load data, do some operations, close Workbook
Application.ScreenUpdating = True
Application.EnableEvents = True
Thanks but I already using those declarations, that is not the problem.
Public Sub TLD_StartMacro()
With Application
.ScreenUpdating = False
.Calculation = Excel.xlCalculationManual
.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
.CutCopyMode = False
.DisplayAlerts = False
End With
End Sub
Public Sub TLD_EndMacro()
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.Calculation = Excel.xlAutomatic
.EnableEvents = True
.CutCopyMode = False ' Esta sentencia vacĂa el portapapeles
End With
End Sub
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.