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

前缀 :我的代码打开一个外部工作簿,里面有一个DB,里面有一些信息,这些信息对整个组织来说都是不可见的。 我能够打开外部工作簿,并成功从PivotTable检索所有数据。

问题 :当我的代码运行时,屏幕闪烁约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 

为了尽量减less屏幕闪烁,我认为以下应该工作; 一旦ScreenUpdating被closures,隐藏ActiveWindow的额外步骤就是添加,以便在重置可见性级别之前打开和隐藏工作簿。 当我尝试它,function区似乎停用和激活,但电子表格保持无闪烁。 不知道这是否足以改善你的身体…

 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 

另一个解决方法是,如果你喜欢,那就行了。 这个想法是,你复制工作簿中的数据透视表的工作表,你从那里工作。 将表单隐藏起来,一旦你准备好了,删除它。

 Option Explicit Sub ImportSheet() Dim wbBk As Workbook Dim wsSht As Worksheet Dim sImportFile As String Dim sFile As String Dim sThisBk As Workbook Dim vfilename As Variant Application.ScreenUpdating = False Application.DisplayAlerts = False Set sThisBk = ActiveWorkbook sImportFile = Application.GetOpenFilename( _ FileFilter:="Microsoft Excel Workbooks, *.xls; *.xlsx", Title:="Open Workbook") vfilename = Split(sImportFile, "\") sFile = vfilename(UBound(vfilename)) Application.Workbooks.Open Filename:=sImportFile Set wbBk = Workbooks(sFile) If SheetExists("MyPivotData") Then Set wsSht = wbBk.Sheets("MyPivotData") wsSht.Copy ThisWorkbook.Sheets("MyPivotData").Visible = xlSheetVeryHidden End If 'do your work 'then delete the added sheet wbBk.Close SaveChanges:=False Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub Private Function SheetExists(sWSName As String) As Boolean Dim ws As Worksheet On Error Resume Next Set ws = Worksheets(sWSName) If Not ws Is Nothing Then SheetExists = True End Function