Matth
Matth

Reputation: 159

Excel ScreenUpdating False and still flickering screen

I have the following simple code to close a range of open workbooks. I have just switched to Excel 2013 and in this new version my screen keeps flashing a white window in Excel for each workbook that is unhidden.

How can I get that annoying screen flicker to shut off?

Sub CloseFiles()
    On Error Resume Next

    Application.ScreenUpdating = False
    Application.StatusBar = "Please wait while files are closed."
    Application.DisplayAlerts = False

    Dim rCell As Range
    For Each rCell In Range("Files")
      Application.StatusBar = "Closing file " & rCell.Value
      If rCell.Value <> "" Then
         Windows(rCell.Value).Visible = True
         Workbooks(rCell.Value).Close SaveChanges:=True
      End If
    Next rCell

    Application.WindowState = xlMaximized
    Windows("Filename.xlsm").Activate

    Application.DisplayAlerts = True
    Application.StatusBar = False
    Application.ScreenUpdating = True

End Sub

Upvotes: 9

Views: 44018

Answers (6)

Having read most of the answers with potential solutions, I'm sorry to tell you none of them worked for me when trying to stop the flickering at the moment of making a worksheet visible/invisible.

Then, it came up to me that the flickering could be caused by the automatic recalculation of the workbook, so I gave it a try, AND IT WORKED!

This is what I'm using:

 Private Sub StopFlickering (ws As Worksheet)

  Application.Calculation = xlManual
  Application.ScreenUpdating = False

  'Make the worksheet visible/invisible according to previous condition
  'Instead, use "= True" or "= False", if it is the specific case
  ThisWorkbook.Worksheets(ws.Name).Visible = _ 
    Not ThisWorkbook.Worksheets(ws.Name).Visible

  '(any other code in here)

  'Don't forget to restore previous settings
  Application.ScreenUpdating = True  
  Application.Calculation = xlAutomatic

End Sub

Example to use it from anywhere in the workbook:

StopFlickering ThisWorkbook.Worksheets("Worksheet Name")

I hope it works not just for me, but for anyone who gives it a try. Good luck, and let me know.

Upvotes: 3

jblood94
jblood94

Reputation: 16981

Use WindowState in combination with DisplayAlerts. The user won't see the window minimize, but it will also keep Excel from flickering during a SaveAs, changing window visibility, or changing workbook/worksheet protection.

Dim iWindowState as Integer

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    iWindowState = .WindowState
    .WindowState = xlMinimized
End With

'Flickery code

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .WindowState = iWindowState
End With

Upvotes: 6

Dean Meyer
Dean Meyer

Reputation: 31

Protecting/unprotecting a sheet activates the sheet, despite the setting for Application.ScreenUpdating. This was the cause for my flicker.

Upvotes: 3

Excelerator_Marc
Excelerator_Marc

Reputation: 11

I have determined the easiest way to solve this is to call the code from a separate subroutine.

Sub startcode()
     Application.ScreenUpdating = False
     Call myrunningsub()
     Application.ScreenUpdating = True
End Sub

Sub myrunningsub()
    'your code here
End Sub

This seems to work in binding the Application.ScreenUpdating parameter. Note I use Excel 2013 in Windows 7 64 bit.

Upvotes: 1

Patrick Lepelletier
Patrick Lepelletier

Reputation: 1654

i Had the same "problem", and i use this kind of code (i add a doevents, and a .displaystatusbar=true)

Sub CloseFiles()
err.clear
On Error Resume Next

with Application
    .ScreenUpdating = False
    .displaystatusbar = true 'kinda need this line
    .StatusBar = "Please wait while files are closed." 
    doevents 'magic trick
    .DisplayAlerts = False
    .calculation= xlManual  'sometimes excel calculates values before saving files
    .enableevents=false  'to avoid opened workbooks section open/save... to trigger
end with

'code


with application
        .StatusBar = False
        .displaystatusbar = false
        .DisplayAlerts = True   
        .ScreenUpdating = True
        .enableevents=true
        .calculation= xlAutomatic
end with

End Sub

Upvotes: 0

whytheq
whytheq

Reputation: 35557

As Siddharth mentioned (I was just a second behind) ....why bother making the books visible - just close each and save changes.

Couple of other points:
1. I used to play around with the applications StatusBar but don't bother anymore - too many times where the application crashes mid-procedure and the bar is left with an unwanted message on it!
2. Is On Error Resume Next required in the flow of the program e.g. are you using it to deliberately hit an error and then move on to the next line of code? If not then just hiding an error can be dangerous....sometimes best to let programs error and then you know where you stand and can then fix the problem.

Sub CloseFiles()
    On Error Resume Next

    Application.ScreenUpdating = False
    Application.StatusBar = "Please wait while files are closed."
    Application.DisplayAlerts = False

    Dim rCell As Range
    For Each rCell In Range("Files")
      Application.StatusBar = "Closing file " & rCell.Value
      If rCell.Value <> "" Then
         'Windows(rCell.Value).Visible = True  '::::::::why bother with this?
         Workbooks(rCell.Value).Close SaveChanges:=True 
      End If
    Next rCell

    Application.WindowState = xlMaximized
    Windows("Filename.xlsm").Activate

    Application.DisplayAlerts = True
    Application.StatusBar = False
    Application.ScreenUpdating = True

End Sub

Upvotes: 0

Related Questions