Sha*_*ado 6 excel vba excel-vba
前缀:我的代码打开一个外部工作簿,里面有一个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
Run Code Online (Sandbox Code Playgroud)
为了最大限度地减少屏幕闪烁,我认为以下方法应该有效;它添加了一个额外步骤,即在关闭 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1373 次 |
| 最近记录: |