1

I am currently attempting to optimize some code. Currently, I use ActiveWorkbook.RefreshAll which slows the whole run time down significantly, in actual fact I only need to refresh two worksheet in the workbook. Does a function such as Worksheet("Name").RefreshAll exist? I am aware of .calculate, but I don't think it will update my pivot tables as well as calculation in the given sheets.

Regards

braX
  • 11,506
  • 5
  • 20
  • 33
  • Possible duplicate [here](https://stackoverflow.com/questions/154434/how-do-you-get-excel-to-refresh-data-on-sheet-from-within-vba). – Pat Jones Dec 14 '17 at 17:45

1 Answers1

1

Why not loop through your workbook, and on those specific sheets, calculate and refresh the pivot table? If the tables aren't on the same sheet, you can tweak the code to loop through pivot tables and refresh that way, instead of within the "each sheet" loop.

Sub t()
Dim ws As Worksheet
Dim pvt As PivotTable

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = "Sheet4" Or ws.Name = "Sheet2" Then
        ws.Calculate
        For Each pvt In ws.PivotTables
            pvt.RefreshTable
        Next pvt
    End If
Next ws

End Sub
BruceWayne
  • 22,923
  • 15
  • 65
  • 110