Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel ScreenUpdating False and still flickering screen

Tags:

excel

vba

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
like image 280
Matth Avatar asked Feb 12 '14 07:02

Matth


4 Answers

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
like image 170
jblood94 Avatar answered Sep 25 '22 10:09

jblood94


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.

like image 43
Prof. Juan Manuel Alonso D. Avatar answered Sep 24 '22 10:09

Prof. Juan Manuel Alonso D.


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

like image 36
Dean Meyer Avatar answered Sep 26 '22 10:09

Dean Meyer


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.

like image 27
Excelerator_Marc Avatar answered Sep 23 '22 10:09

Excelerator_Marc