Application.ScreenUpdating toggle - excel

I'm looking for the simplest way to toggle ScreenUpdating via call.
Can you please advise if this solution could have any adverse effects?
Sub Toggle_Screen_Updating()
With Application
.ScreenUpdating = IIf(.ScreenUpdating, False, True)
End with
End Sub

This will toggle it.
Application.ScreenUpdating = Not Application.ScreenUpdating
I recommend not to use toggleing because you never clearly know the state after running this line. You only know that is is the opposite that it was before.
I recommend to explicitly run Application.ScreenUpdating = False whenever you want to turn it off and Application.ScreenUpdating = True whenever you want to turn it on.
I see no advantage in using a toggle instead.

Related

Why Application.CommandBars("Standard").Controls("&Undo") is disabled in a particular sheet?

I cannot figure out why Application.CommandBars("Standard").Controls("&Undo") is disabled in a particular sheet.
I also tried to explicitely enable it with:
Application.CommandBars("Standard").Controls("&Undo").Enabled = True
But still no results:
Dim lastAction As String
If Application.CommandBars("Standard").Controls("&Undo").Enabled = True Then
MsgBox "It works"
lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(lastAction, 5) = "Paste" Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Call MasterPageFixer
Application.ScreenUpdating = True
Application.EnableEvents = True
Else
Exit Sub
End If
End If
Any idea about how to programmatically make sure that this sheet could keep the possibility of accessing the undo list?
The Application.CommandBars("Standard").Controls("&Undo").Enabled refers to the standard arrows for "Undo" in Excel. These are not per sheet, but per application, as the line says. They arrows are enabled/disabled if there is a possibility to use them. Thus, the "disabled" stands only if there is nothing to be "undone". E.g., check the following steps:
Open a new Excel workbook and run this in the Immediate window. You will see False:
This is because there is nothing to be "undone":
Then, if you write some text in the excel cells and run the code from the immediate window again, you will see True. Additionally, the arrow will look like this:

How can I keep excel from "Not Responding", when running my refresh macro?

Through research, it seemed all I needed to do was use this? But my screen still bugs out while refreshing. I am just wanting the screen to not change or do anything at all when running this macro.
Application.ScreenUpdating = False
Here is just a small piece of my code that runs in this macro for reference:
Sub Refresh_Data()
Box.Show
Application.Cursor = xlWait
Application.ScreenUpdating = False
Workbooks("IOM Denial.xlsm").RefreshAll
Workbooks("IOM Denial.xlsm").Worksheets("Home").Activate
Application.Cursor = xlDefault
Application.ScreenUpdating = True
Unload Box
End Sub
You are showing the box in a modal form, Excel is waiting for a response from you before proceeding any further than Box.Show. If you close the Box using the little cross, your code should continue to run.
You can fix this in your code by changing the line to
Box.Show False '<-- runs non-modal
Information from Microsfot here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/showmodal-property

Excel slow with other spreadsheets open

I have a very simple VBA code running in workbook A that is rather slow when another specific workbook B is open. I don't have access to any macros inside workbook B, so I can't know for sure if anything wierd is happening there.
I've looked around and came with this minimal code condensing the typical solutions I've found in stackoverflow and many Excel specific sites, but it still takes a long time to run.
' Code in workbook A
Sub slow_simple_macro()
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
.DisplayStatusBar = False
End with
Workbooks("workbookA.xlsm").Sheets("Sheet1").Range("A1") = "Slow" ' This line takes about half a second to run when workbook B is open
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
.DisplayStatusBar = True
End with
End Sub
I'm guessing that disabling events is not enough to prevent something inside workbook B from running. Are there any other Application flags I can setup to make it run faster? Any other ideas?
You can see while disabling some functions of excel like calculation mode, screenupdates,status bar would impact on application of Excel because "Excel is the one of The object model that is a large hierarchy of all the objects used in VBA" (Source:www.globaliconnect.com).
So if you want run by turn off application functions , you should be wait till you set back to its original or else same problem would happen.
Better simplify code as possible unless if its required
Sub slow_simple_macro()
Workbooks("workbookA.xlsm").Sheets("Sheet1").Range("A1") = "Slow"
End Sub
Hope you find it as helpful. Have a good day.

Screenupdating not flipping

In Excel 2010 the method described below, the ScreenUpdating works correctly. But in 2007, it doesnt flip and the worksheet operations are visually being seen.
VBA Usage:
Dim scrup As Boolean: scrup = DisableScreenUpdating(Application.ScreenUpdating)
Method Declaration:
Function DisableScreenUpdating(val As Boolean) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''
' Disable ScreenUpdating, for seemless operation
If val Then
Application.ScreenUpdating = False
End If
'''''''''''''''''''''''''''''''''''''''''''''''''
DisableScreenUpdating = val
End Function
Question:
What am i missing in 2007 that 2010 is either assuming or is working correctly?
Still tracking down the bug cause it is still happening on 1 version of the file but other two versions it will not. The Versions all have the same code-base but based on various settings change representation to the end-user(s).
NOTE:
Please DO NOT focus on the "Why i am doing this", and more of what situations would cause the ScreenUpdating method to NOT be changed from True to False.
You could try eliminating the conditional and see if the problems is still there. That ways you'd know if it has something to do with conditional or not or 'val'.
Function DisableScreenUpdating() As Boolean
Application.ScreenUpdating = False
DisableScreenUpdating = True
End Function
Assuming this makes your bug go away, I'd then focus on the call....
DisableScreenUpdating(Application.ScreenUpdating)
Perhaps the bug something to do with reading the ScreenUpdating property, followed shortly by write. That's just a guess though.
Also, I'd search your project for any other usage of Application.ScreenUpdating. There may be some other code causing the updating to return to True.
For one thing, is there a function to enable ScreenUpdating? Your code only disables it. Here is your code modified with code that I use:
Dim scrup As Boolean: scrup = DisableScreenUpdating(Not Application.ScreenUpdating)
'Disable ScreenUpdating, for seemless operation
Function DisableScreenUpdating(val As Boolean) As Boolean
With Application
If .ScreenUpdating = val Then 'Gets rid of flashes for changing it to the same
.ScreenUpdating = Not val
End If
'Doesn't matter what happens above, this is based on what Excel would send back
DisableScreenUpdating = Not .ScreenUpdating
End With
End Function
I have seen the bug...yet the statement works, only the VBA-editor doesnt see changes.
However you can see the change by assigning a variable and then read it in the VBA-editor.
>Sub testing()
>Dim i As Long
>
>Application.ScreenUpdating = False
>i = Application.ScreenUpdating
>MsgBox "Assigned to i...real value = " & CBool(i)
>
>Application.ScreenUpdating = True
>i = Application.ScreenUpdating
>MsgBox "Value of Application.ScreenUpdating = " & CBool(i)
>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