excel - 打开第二个工作簿(同一实例)时防止屏幕闪烁

标签 excel vba

前缀:我的代码打开一个外部工作簿,其中有一个数据库,其中包含一些整个组织不应该看到的信息。我能够打开外部工作簿,并成功从数据透视表检索所有数据。

问题:当我的代码运行时,屏幕闪烁约 0.5 秒以显示其他工作簿。

目标:在工作簿之间切换时屏幕上不出现任何闪烁。

我的代码(相关部分):

Option Explicit

Public Sub GetBudgetData_fromPivotTable(Budget_ShtName As String, Budget_PvtName As String)

Dim BudgetWB                            As Workbook
Dim PvtTbl                              As PivotTable
Dim pvtFld                              As PivotField
Dim strPvtFld                           As String
Dim prjName                             As String

' ****** This is the Section I am trying to prevent from the screen to flicker ******
Application.ScreenUpdating = False
Application.DisplayAlerts = False

' read budget file parameters
Set BudgetWB = Workbooks.Open(BudgetFile_Folder & BudgetFile_wbName)

BudgetWB.Windows(1).Visible = False
OriginalWB.Activate ' <-- this is the original workbook that is calling the routine

Set PvtTbl = BudgetWB.Worksheets(Budget_ShtName).PivotTables(Budget_PvtName)

' a lot of un-relevant code line

BudgetWB.Close (False)  ' close budget file
OriginalWB.Activate

' restore settings
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

最佳答案

为了最大限度地减少屏幕闪烁,我认为以下方法应该有效;它添加了一个额外步骤,即在关闭 ScreenUpdating 后隐藏 ActiveWindow,以允许在重置可见性级别之前打开和隐藏工作簿。当我尝试时,功能区似乎会停用和激活,但电子表格保持不闪烁。不确定这对您来说是否足以改善...

Public Sub GetBudgetData_fromPivotTable(Budget_ShtName As String, Budget_PvtName As String)

    Dim BudgetWB                            As Workbook
    Dim PvtTbl                              As PivotTable
    Dim pvtFld                              As PivotField
    Dim strPvtFld                           As String
    Dim prjName                             As String

    ' ****** This is the Section I am trying to prevent from the screen to flicker ******
    Dim wbWindow As Window: Set wbWindow = ActiveWindow
    ' Freeze current screen
    Application.ScreenUpdating = False
    wbWindow.Visible = False

    ' read budget file parameters
    Set BudgetWB = Workbooks.Open(BudgetFile_Folder & BudgetFile_wbName)
    BudgetWB.Windows(1).Visible = False

    ' Reset current screen
    wbWindow.Visible = True
    Application.ScreenUpdating = True

    OriginalWB.Activate ' <-- this is the original workbook that is calling the routine

    Set PvtTbl = BudgetWB.Worksheets(Budget_ShtName).PivotTables(Budget_PvtName)

    ' a lot of un-relevant code line

    BudgetWB.Close (False)  ' close budget file
    OriginalWB.Activate

    ' restore settings
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

关于excel - 打开第二个工作簿(同一实例)时防止屏幕闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42372340/

相关文章:

vba - 为什么 Microsoft Word 2007 中的 VBA IDE 的 Intellisense 不断更改特定变量类型名称的大小写?

vba - 如何使用 VBA 将“是/否”复选框列添加到现有表?

excel - 在命名范围VBA中选择单元格到最后填充的行

javascript - 使用 D3 解析我的 CSV

excel - "range("F :F")"mean in an Excel macro? 是什么意思

java - 在新的 Excel 工作表中写入行

vba - 通过vba将具有数据列的行转换为Excel 2003中具有多行的列

excel - 我的代码出现 "Permission Denied"错误

vba - 使用 Sheets.Range.Offset 的行上的应用程序或对象定义的错误

vb.net - 在Excel电子表格中间放置多个滚动条