前缀:我的代码打开一个外部工作簿,其中有一个数据库,其中包含一些整个组织不应该看到的信息。我能够打开外部工作簿,并成功从数据透视表
检索所有数据。
问题:当我的代码运行时,屏幕闪烁约 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/