Prefix: My code opens an external workbook that has a DB inside with some information which should not be visible to the entire organization. I am able to open the external workbook, and retrieve all the data from the PivotTable
successfully.
Problem: When my code runs, the screen flickers for ~ 0.5 seconds to show the other workbook.
Goal: not to have any flickering on the screen when switching between workbooks.
My Code (relevant section):
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
To minimize screen flicker, I think the following should work; it is adding in the additional step of hiding the ActiveWindow once ScreenUpdating has been turned off to allow the workbook to be opened and hidden before resetting the visibility levels. When I tried it, the ribbon appears to deactivate and activate, but the spreadsheet stayed flicker-free. Not sure if this is enough of an improvement for you...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With