Excel ScreenUpdating错误和仍然闪烁的屏幕

Mat*_*tth 6 excel vba excel-vba

我有以下简单的代码来关闭一系列打开的工作簿。我刚刚切换到Excel 2013,在此新版本中,对于未隐藏的每个工作簿,我的屏幕在Excel中始终闪烁一个白色窗口。

如何使烦人的屏幕闪烁关闭?

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
Run Code Online (Sandbox Code Playgroud)

jbl*_*d94 6

WindowState与 结合使用DisplayAlerts。用户不会看到窗口最小化,但它也会防止 Excel 在SaveAs更改窗口可见性或更改工作簿/工作表保护期间闪烁。

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
Run Code Online (Sandbox Code Playgroud)


why*_*heq 0

正如 Siddharth 提到的(我只落后一秒)......为什么要费心让书可见 - 只需关闭每本书并保存更改即可。

其他几点:
1. 我曾经玩过这些应用程序StatusBar,但不再打扰了 - 很多次应用程序在过程中崩溃,并且栏上留下了不需要的消息!
2.On Error Resume Next在程序流程中是必需的,例如,您是否使用它来故意出错,然后继续执行下一行代码?如果不是,那么仅仅隐藏错误可能是危险的……有时最好让程序出错,然后您知道自己的立场并可以解决问题。

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
Run Code Online (Sandbox Code Playgroud)