Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Screen flickering when opening a second Workbook (same instance)

Tags:

excel

vba

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
like image 979
Shai Rado Avatar asked Feb 21 '17 16:02

Shai Rado


1 Answers

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
like image 75
Tragamor Avatar answered Nov 07 '22 03:11

Tragamor