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

时间:2014-02-12 07:36:36

标签: excel excel-vba 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

6 个答案:

答案 0 :(得分:3)

通过潜在的解决方案阅读了大部分答案后,我很遗憾地告诉你,当我试图在工作表可见/不可见时停止闪烁时,它们都没有为我工作。

然后,我发现闪烁可能是由于工作簿的自动重新计算造成的,所以我试了一下,然后工作了!

这就是我正在使用的:

  

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

从工作簿中的任何位置使用它的示例:

  

StopFlickering ThisWorkbook.Worksheets(“工作表名称”)

我希望它不仅适用于我,也适用于任何尝试过的人。祝你好运,让我知道。

(PS。:我希望这个答案的格式符合规则。这是我第一次在任何地方做这样的事情。我提前为任何错误道歉。)

答案 1 :(得分:1)

我已经确定解决此问题的最简单方法是从单独的子例程调用代码。

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

Sub myrunningsub()
    'your code here
End Sub

这似乎可以用于绑定Application.ScreenUpdating参数。注意我在Windows 7 64位中使用Excel 2013。

答案 2 :(得分:1)

尽管已设置Application.ScreenUpdating,但保护/取消保护工作表会激活工作表。这是我闪烁的原因。

答案 3 :(得分:1)

WindowStateDisplayAlerts结合使用。用户不会看到窗口最小化,但它也会使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

答案 4 :(得分: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

答案 5 :(得分:0)

我有同样的“问题”,我使用这种代码(我添加一个doevents,和.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