SOLVED

Pivot Table Data 'Refresh data when opening file'

%3CLINGO-SUB%20id%3D%22lingo-sub-3246625%22%20slang%3D%22en-US%22%3EPivot%20Table%20Data%20'Refresh%20data%20when%20opening%20file'%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3246625%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20looking%20for%20a%20VBA%20that%20runs%20as%20a%20macro%20once%20and%20ticks%20the%20'Refresh%20Data%20when%20opening%20the%20file'%20for%20every%20pivot%20table%20in%20the%20workbook%20rather%20than%20having%20to%20do%20it%20manually.%26nbsp%3B%20I%20saw%20this%20code%20that%26nbsp%3B%3CSPAN%3Econverts%20all%20the%20fields%20in%20a%20selected%20pivot%20table%20to%20the%20count%20function%20or%20other%20functions%20with%20VBA%20Macro%20in%20Excel%20and%20wondered%20if%20this%20could%20be%20changed%20to%20tick%20the%20'Refresh%20Data%20when%20opening%26nbsp%3Bthe%20file'%20box%20instead%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThe%20reason%20I'm%20looking%20for%20this%20is%20that%20I%20create%20a%20daily%20report%20and%20every%20day%20the%20data%20source%20has%20to%20be%20changed%20and%20I%20currently%20use%20a%20VBA%20to%20do%20this%20for%20the%20entire%20worksheet.%20I%20then%20manually%20ticked%20'refresh%20data%20when%20opening%26nbsp%3Bthe%20file'%20in%20every%20pivot%20which%26nbsp%3Bworked%20great%2C%20but%20then%20as%20soon%20as%20the%20changed%20the%20data%20source%20the%20next%20day%2C%20the%20boxes%20unchecked%3F%3C%2FSPAN%3E%3C%2FP%3E%3CPRE%3EPublic%20Sub%20PivotFieldsToCount()%0A%20%20%20%20Dim%20pf%20As%20PivotField%0A%20%20%20%20With%20Selection.PivotTable%0A%20%20%20%20%20%20%20%20.ManualUpdate%20%3D%20True%0A%20%20%20%20%20%20%20%20For%20Each%20pf%20In%20.DataFields%0A%20%20%20%20%20%20%20%20%20%20%20%20With%20pf%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Function%20%3D%20xlCount%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.NumberFormat%20%3D%20%22%23%2C%23%230%22%0A%20%20%20%20%20%20%20%20%20%20%20%20End%20With%0A%20%20%20%20%20%20%20%20Next%20pf%0A%20%20%20%20%20%20%20%20%20.ManualUpdate%20%3D%20False%0A%20%20%20%20%20End%20With%0A%20End%20Sub%3C%2FPRE%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20560px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F352821i4E01EFFCC048A23B%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EKind%20regards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECharlotte%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3246625%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hi,

 

I'm looking for a VBA that runs as a macro once and ticks the 'Refresh Data when opening the file' for every pivot table in the workbook rather than having to do it manually.  I saw this code that converts all the fields in a selected pivot table to the count function or other functions with VBA Macro in Excel and wondered if this could be changed to tick the 'Refresh Data when opening the file' box instead?

 

The reason I'm looking for this is that I create a daily report and every day the data source has to be changed and I currently use a VBA to do this for the entire worksheet. I then manually ticked 'refresh data when opening the file' in every pivot which worked great, but then as soon as the changed the data source the next day, the boxes unchecked?

Public Sub PivotFieldsToCount()
    Dim pf As PivotField
    With Selection.PivotTable
        .ManualUpdate = True
        For Each pf In .DataFields
            With pf
                .Function = xlCount
                .NumberFormat = "#,##0"
            End With
        Next pf
         .ManualUpdate = False
     End With
 End Sub

image.png

Kind regards

 

Charlotte

 

2 Replies
best response confirmed by clh_1496 (Occasional Contributor)
Solution

 

Sub RefreshOnOpenAll()
    Dim pvc As PivotCache
    For Each pvc In ActiveWorkbook.PivotCaches
        pvc.RefreshOnFileOpen = True
    Next pvc
End Sub

@clh_1496 

Amazing, thank you!