Keeping Application.ScreenUpdating = False After Running Macro - excel

I'm trying to create a hotkey that will toggle Application.ScreenUpdating. I just learned that the newer Excel versions automatically reset Application.ScreenUpdating to true after running a macro which is good 99.99% of the time except I don't want that to happen I want it to be persistent. Is there anyway for me to keep Application.ScreenUpdating = False?
Sub suspend_screen_update()
MsgBox "1st " & Application.ScreenUpdating
If Application.ScreenUpdating = True Then
Application.ScreenUpdating = False
Else
Application.ScreenUpdating = True
End If
MsgBox "2nd " & Application.ScreenUpdating
End Sub
Private Sub hk_toggle_screen_update()
Application.OnKey "{F1}", suspend_screen_update
End Sub
Why? My boss sometimes request that I only use Excel, nothing else, to manipulate a lot of data. Excel can be super slow if not crash when the data gets really big. I'm trying to save us time by creating this hotkey.

Related

Excel application won't quit after checking in to sharepoint

I am having trouble quitting the excel application after checking in to sharepoint. I have the following code in the thisworkbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = False
Application.ScreenUpdating = False
ThisWorkbook.Save
If Workbooks.Count < 2 Then
Application.Quit
Else
ThisWorkbook.Close
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
If ThisWorkbook.CanCheckIn Then
ThisWorkbook.CheckIn
Else
MsgBox ("This workbook cannot be checked in.")
End If
End Sub
The file is successful in checking in to sharepoint. The workbook gets closed but the excel application is still running. How do I terminate the application?
Thanks.
Application.Quit
Should do the trick. Add that when you want to close Excel.
I've been having a similar problem and I found a quick discussion on what could be causing the Excel instance to not quit.
Excel won't quit
And here is an absolutely awful way to get around having to properly code.
How Can I Kill Task Manager Processes Through VBA code

excel vba slowing excel down, causing 10 second egg timer delay when clicking anywhere on sheet

I am using the following vba codes which im using to hide a set of rows and unhide rows depending on if a cell contains text or not, and they are causing my excel spreadsheet to be slow and unresponsive and causing the egg timer to show for about 10 seconds.
If I take the code out It speeds things up so what can I do to my codes to get them to speed up and not take so long? perhaps there is a better way of structuring the code but im really new to vba so am not sure what I would need to do, would appreciate someone's help thanks.
the reason I am using worksheet change and worksheet selection change is so that whether a user clicks on a cell or not the page still updates
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
The main issue is from the Worksheet_Change event, but it could be applied to any event.
The worksheet change is triggering each time you hide a column, so it's trying several times to hide the same columns, before (eventually) failing with an out of memory error:
Hide these columns... Oh, a worksheet change... Hide these columns... Oh, A worksheet change... Hide th...
To avoid this, you need to use
Application.EnableEvents = False
when you decide you are going to make changes, and
Application.EnableEvents = True
when done.
You may also want to put some error handling that turns the events on again, as if something else occurs that stops the code from running, the triggers will be turned off, and the spreadsheet will no longer update as you expect it to.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("K22").Value <> "" Then
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This works instantly for me:
Application.ScreenUpdating = False
Select Case Range("K22")
Case Is <> ""
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Case Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End Select
Application.ScreenUpdating = True

Application.Quit command not closing the entire Excel Application

I have following code under a button. When clicked it just closes the current Excel sheet but not the entire Excel application.
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
Application.Quit
Note: I don't have any other sheets open.
The following window still appears.
I had this issue and I resolved it by putting in the Workbook_BeforeClose():
ThisWorkbook.saved = true
I experienced the same issue and was able to resolve the issue with code that looks to see if multiple workbooks are open or not ...
Application.EnableEvents = False
Application.DisplayAlerts = False
If Application.Workbooks.Count = 1 Then 'Close Excel application
ThisWorkbook.Save
Application.Quit
Else 'Close the active workbook
With ActiveWorkbook
.Close Savechanges:=True
End With
End If
When Application.Quit is encountered in a subroutine,
it will only stay in memory and continue to run lines under it
and will actually quit until it encounters a "Exit Sub".
When the normal "End Sub" at the primary level is encountered,
it will then also close Excel. But say if the workbook is somehow
closed before reaching the "Exit Sub", "End" or "End Sub" line, then
Excel will not close.
Solution is to create a Public variable called ToQuitNow
with initial False value
and change it to True where you want Excel to quit.
and test right after to see if it is true, then return to previous Sub level
by "Exit Sub" or "End" to quit right away,
and do the same at every subrountine level where
it is expected to return from the deeper subroutine.
When it gets back to the primary level,
then a final "Exit Sub" will actually terminates Excel.
If you do not want Excel to ask for saving changes made,
add line "ThisWorkbook.Saved = True" right after Application.Quit,
or before the final "Exit Sub" at the Primary level
and Excel will quit without saving.
Try the following test below, just run "Test"
Public ToQuitNow As Boolean
Sub Test()
ToQuitNow = False ' initialize with False value
Call SecondSub
MsgBox ("Primary level here. Back from SecondSub")
If ToQuitNow = True Then
Exit Sub 'will actually quit Excel now if True
End If
MsgBox ("This line will not run if ToQuitNow is True")
End Sub
Sub SecondSub()
MsgBox ("SecondSub here")
Call ThirdSub
MsgBox ("SecondSub here. Back from ThirdSub")
If ToQuitNow = True Then
Exit Sub ' will return to Main level if True
End If
MsgBox ("This line from SecondSub will not run if ToQuitNow is True")
End Sub
Sub ThirdSub()
MsgBox ("ThirdSub here")
Call FourthSub
MsgBox ("ThirdSub here. Back from FourthSub")
If ToQuitNow = True Then
Exit Sub ' will return to SecondSub if True
End If
MsgBox ("This line from ThirdSub will not run if ToQuitNow is True")
End Sub
Sub FourthSub()
MsgBox ("FourthSub here")
Application.Quit
ThisWorkbook.Saved = True ' Excel will think changes already saved _
and will quit without saving
ToQuitNow = True ' activate Quit
If ToQuitNow = True Then
MsgBox ("Quit command executed in FourthSub")
Exit Sub ' will return to ThirdSub if True
'Can also put in End in above line to quit right away
End If
MsgBox ("This line from FourthSub will not run if ToQuitNow is True.")
End Sub
remove the Application.DisplayAlerts = True from the routine.
from the help for Application.Quit Method:
If unsaved workbooks are open when you use this method, Microsoft Excel displays a dialog box asking whether you want to save the changes. You can prevent this by saving all workbooks before using the Quit method or by setting the DisplayAlerts property to False. When this property is False, Microsoft Excel doesn’t display the dialog box when you quit with unsaved workbooks; it quits without saving them.
This will avoid any (possibly hidden) prompts from stopping excel from closing completely
I did not try it, but maybe this will help:
https://www.mrexcel.com/forum/excel-questions/606195-vba-application-quit-not-working-me-completly-why.html
According to Norie you might not have anymore workbooks open, therefore Application.Quit will never be executed.
AlphaFrog therefore suggests this:
Application.DisplayAlerts = False
If Application.Workbooks.Count = 1 Then
Application.Quit
Else
ActiveWorkbook.Close
End If
The window does not close because you are using personal.xlsb. Cut Personal.xlsb and paste in another location.
Instead of Personal.xlsb create and work on modules. It's a better option.
"ThisWorkbook.Saved = True" after "Application.Quit" works on Excel 2016
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Quit
ThisWorkbook.Saved = True
End Sub
I had the same issue using the following code closed excel cleanly:
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.Quit
This will allow excel to cleanly close without keeping a "ghost" window open.
This worked for me: (Office 365)
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
Application.EnableEvents = False
ThisWorkbook.Save
Application.Quit
ThisWorkbook.Saved = True
End Sub
This is a strange one, hopefully someone will find this answer useful. I ran into something very similar using Excel 2010 (14.0). I stumbled to my answer through experimentation. This is bad answer for general purpose.
For whatever reason Application.Quit fails silently if the option AccessVBOM is not enabled. It is not enabled out of the box and can be set/unset by your network admin by windows policy.
You can find this option in the GUI by traversing "Excel Options" -> "Trust Center" -> "Trust Center Settings" -> "Macro Settings" -> "Trust access to the VBA project object model". Or programmatically.
.
Since we all love code, in this example we are running Excel from C# interop and calling the quit function.
using Excel = Microsoft.Office.Interop.Excel;
using Marshal = System.Runtime.InteropServices.Marshal;
Excel.Application app = new Excel.Application();
app.Visible = false;
app.DisplayAlerts = false;
// this will hang if AccessVBOM is not enabled
app.Quit();
Marshal.ReleaseComObject(app);
Have passed MacroName from bat file and tried the below code its working. But one thing I observed is if we are closing the workbook(ActiveWorkbook.Close) before Application.Quit then it is not working.
Private Sub Auto_Open()
Dim strMacroName As String
strMacroName =
VBA.CreateObject("WScript.Shell").Environment("process").Item("MacroName")
If strMacroName <> "" Then Run strMacroName
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.DisplayAlerts = True
If strMacroName <> "" Then Application.Quit
End Sub
Sub Button1_Click()
MsgBox ("done")
End Sub
Make sure that your sheets do not have any external link references, especially broken links.
I struggled with this problem for more than a week, rewriting and commenting out lots of code to try to isolate the problem. I finally did a review of all table and external sheet references in my workbook this morning. I removed all unnecessary links and broken references and the workbook now closes without hanging in memory.

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

Make window open in background

I have 3 input boxes that when a document is selected it opens the document and fills in a select box. With Excel 2013 these windows are opened in front of the document and have to be minimized in order to select the next one. Is there a way to have it automatically open documents in the background?
Workbooks.Open (file_path)
If Application.Version >= 15# Then
ActiveWindow.WindowState = xlMinimized
End If
This what I currently have, however I would rather it open in the background in the first place over having to minimize it. I have tried to turn off screen updating for the part, however that did not work.
Application.ScreenUpdating = False
Workbooks.Open (file_path)
Application.ScreenUpdating = True
Does Application.ScreenUpdating affect WorkBooks.open in Excel 2013?
Application.ScreenUpdating will not work with excel 2013.
You may try creating function:
Sub myScreenUpdate(screenUpdateRequest As Boolean, previousScreenUpdate As Boolean)
If screenUpdateRequest Then
Application.ScreenUpdating = previousScreenUpdate
Else
previousScreenUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
End If
End Sub
call req:
Call myScreenUpdate(False, previousScreenUpdate)
Call myScreenUpdate(True, previousScreenUpdate)
Dim previousScreenUpdate as boolean
Call myScreenUpdate(False, previousScreenUpdate) 'to get the current setting into previousScreenUpdate
Call myScreenUpdate(True, false) 'to set updating to false
'do your heavy code here and when finished:
Call myScreenUpdate(True, previousScreenUpdate)

Resources