Screenupdating not flipping - excel

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

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:

Application.ScreenUpdating toggle

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.

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

VBA Textbox Autofilter hides everything

I implemented a google like search box into my spreadsheet, using an activex textbox.. However, in the beginning it worked fine, but now it hides the whole used range and i only see blank cells. This really needs to work 100%, but i can't figure out what's the problem.
The code is as follows:
Sub TextBox1_Change()
Application.ScreenUpdating = False
Range("$O$7").AutoFilter Field:=14, Criteria1:=TextBox1
End Sub
I'd appreciate your help with this issue.
I don't see any statement to clear previous filters, for example:
Worksheets(1).AutoFilterMode = False
Also, there should be Application.ScreenUpdating = True before End Sub to restore screen updating.
If TextBox1.vlaue = "" Then Application.Autofiltermode= False

ScreenUpdating doesn't update screen in Excel 2013

I've code that takes 30+ mins to run. I have set screen updating to false while most of the code runs but every now and again I turn it to true and straight back to false.
In 2003, 2007 and 2010 this allows the screen to temporarily update. In 2013 it doesn't work.
How can I make Excel 2013 temporarily update the screen mid-macro?
Sub Test()
Application.ScreenUpdating = False
' Do loads of stuff here
Application.ScreenUpdating = True ' Enable to refresh screen
Application.ScreenUpdating = False ' Disable again
' Do more stuff here
Application.ScreenUpdating = True
End Sub
DoEvents worked for me. I don't know what causes this but inserting DoEvents in the Loop Method seems to correct the issue for me when I use it.
Sub LoopMethod
Foreach i in Identity
Call Loopthis
Next For
EndSub
Sub Loopthis
DoEvents
select.Cells(i, 6)
EndSub
I do place this code right before every event/screen I want to update and works fine for me:
Application.ScreenUpdating = True
DoEvents
Sheets("Main").Select 'Screen to update
Also I put the code inside every loop I use to write someting in the Sheet.
Activating another worksheet and then again activating the required worksheet worked for me - I had a code where the screen updating failed, I activated another worksheet and then again the one I was working on, it updated the current screen.
application.ScreenUpdating = False
''''''code'''''''
Thisworkbook.worksheets(any otherworksheet in workbook).activate
Thisworkbook.worksheets(current worksheet).activate
application.ScreenUpdating = True
As far as I have found the issue can be solved by jumping to another cell. You could do something like this:
Dim Ac as object
Set Ac = ActiveCell
Ac.Offset(0,IIf(Ac.Column = Application.Columns.Count, -1, 1)).Activate
Ac.Activate
As to the how or why of this behaviour I don't know (yet). As far as I have found now, it has something to do with the active cell before deactivating screen update, being the same as after reactivating screen update. But I haven

Resources