<p>I have a workbook with 20 different pivot tables. Is there any easy way to find all the pivot tables and refresh them in VBA?</p>

[{'answer_id': 70976, 'author': 'GSerg', 'author_id': 11683, 'author_profile': 'https://Stackoverflow.com/users/11683', 'pm_score': 9, 'selected': True, 'text': '<p>Yes.</p>\n\n<pre><code>ThisWorkbook.RefreshAll\n</code></pre>\n\n<p>Or, if your Excel version is old enough,</p>\n\n<pre><code>Dim Sheet as WorkSheet, Pivot as PivotTable\nFor Each Sheet in ThisWorkbook.WorkSheets\n    For Each Pivot in Sheet.PivotTables\n        Pivot.RefreshTable\n        Pivot.Update\n    Next\nNext\n</code></pre>\n'}, {'answer_id': 71002, 'author': 'LohanJ', 'author_id': 11286, 'author_profile': 'https://Stackoverflow.com/users/11286', 'pm_score': 1, 'selected': False, 'text': "<p>You have a <em>PivotTables</em> collection on a the VB <em>Worksheet</em> object. So, a quick loop like this will work:</p>\n\n<pre><code>Sub RefreshPivotTables()\n    Dim pivotTable As PivotTable\n    For Each pivotTable In ActiveSheet.PivotTables\n        pivotTable.RefreshTable\n    Next\nEnd Sub\n</code></pre>\n\n<p>Notes from the trenches:</p>\n\n<ol>\n<li>Remember to unprotect any protected sheets before updating the PivotTable.</li>\n<li><strong>Save often</strong>.</li>\n<li>I'll think of more and update in due course... :)</li>\n</ol>\n\n<p>Good luck!</p>\n"}, {'answer_id': 71084, 'author': 'Robert Mearns', 'author_id': 5050, 'author_profile': 'https://Stackoverflow.com/users/5050', 'pm_score': 5, 'selected': False, 'text': "<p>This VBA code will refresh all pivot tables/charts in the workbook.</p>\n\n<pre><code>Sub RefreshAllPivotTables()\n\nDim PT As PivotTable\nDim WS As Worksheet\n\n    For Each WS In ThisWorkbook.Worksheets\n\n        For Each PT In WS.PivotTables\n          PT.RefreshTable\n        Next PT\n\n    Next WS\n\nEnd Sub\n</code></pre>\n\n<p>Another non-programatic option is:</p>\n\n<ul>\n<li>Right click on each pivot table</li>\n<li>Select Table options</li>\n<li>Tick the <strong>'Refresh on open'</strong> option.</li>\n<li>Click on the OK button</li>\n</ul>\n\n<p>This will refresh the pivot table each time the workbook is opened.</p>\n"}, {'answer_id': 359227, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>There is a refresh all option in the Pivot Table tool bar. That is enough. Dont have to do anything else.</p>\n\n<p>Press ctrl+alt+F5</p>\n'}, {'answer_id': 8432023, 'author': 'Steve WahWah Weeks', 'author_id': 1087868, 'author_profile': 'https://Stackoverflow.com/users/1087868', 'pm_score': 3, 'selected': False, 'text': "<p>In certain circumstances you might want to differentiate between a PivotTable and its PivotCache. The Cache has it's own refresh method and its own collections. So we could have refreshed all the PivotCaches instead of the PivotTables.</p>\n\n<p>The difference? When you create a new Pivot Table you are asked if you want it based on a previous table. If you say no, this Pivot Table gets its own cache and doubles the size of the source data. If you say yes, you keep your WorkBook small, but you add to a collection of Pivot Tables that share a single cache. The entire collection gets refreshed when you refresh any single Pivot Table in that collection. You can imagine therefore what the difference might be between refreshing every cache in the WorkBook, compared to refreshing every Pivot Table in the WorkBook.</p>\n"}, {'answer_id': 10434668, 'author': 'Karuna', 'author_id': 1372934, 'author_profile': 'https://Stackoverflow.com/users/1372934', 'pm_score': -1, 'selected': False, 'text': '<p>If you are using MS Excel 2003 then go to view->Tool bar->Pivot Table From this tool bar we can do refresh by clicking ! this symbol.</p>\n'}, {'answer_id': 12478754, 'author': 'RBhandal', 'author_id': 1680524, 'author_profile': 'https://Stackoverflow.com/users/1680524', 'pm_score': -1, 'selected': False, 'text': '<p>I have use the command listed below in the recent past and it seems to work fine.</p>\n\n<pre><code>ActiveWorkbook.RefreshAll\n</code></pre>\n\n<p>Hope that helps.</p>\n'}, {'answer_id': 12592078, 'author': 'Kevin', 'author_id': 1698696, 'author_profile': 'https://Stackoverflow.com/users/1698696', 'pm_score': 5, 'selected': False, 'text': '<p><code>ActiveWorkbook.RefreshAll</code> refreshes everything, not only the pivot tables but also the ODBC queries. I have a couple of VBA queries that refer to Data connections and using this option crashes as the command runs the Data connections without the detail supplied from the VBA</p>\n\n<p>I recommend the option if you only want the pivots refreshed</p>\n\n<pre><code>Sub RefreshPivotTables()     \n  Dim pivotTable As PivotTable     \n  For Each pivotTable In ActiveSheet.PivotTables         \n    pivotTable.RefreshTable     \n  Next \nEnd Sub \n</code></pre>\n'}, {'answer_id': 29474211, 'author': 'user3564681', 'author_id': 3564681, 'author_profile': 'https://Stackoverflow.com/users/3564681', 'pm_score': 0, 'selected': False, 'text': '<p>The code  </p>\n\n<pre><code>Private Sub Worksheet_Activate()\n    Dim PvtTbl As PivotTable\n        Cells.EntireColumn.AutoFit\n        For Each PvtTbl In Worksheets("Sales Details").PivotTables\n        PvtTbl.RefreshTable\n        Next\nEnd Sub \n</code></pre>\n\n<p>works fine.</p>\n\n<p>The code is used in the activate sheet module, thus it displays a flicker/glitch when the sheet is activated.</p>\n'}, {'answer_id': 34544128, 'author': 'Rajiv Singh', 'author_id': 1527856, 'author_profile': 'https://Stackoverflow.com/users/1527856', 'pm_score': 0, 'selected': False, 'text': '<p>Even <strong>we can refresh particular connection</strong> and in turn it will refresh all the pivots linked to it.</p>\n\n<p><strong>For this code I have created slicer from table present in Excel</strong>:</p>\n\n<pre><code>Sub UpdateConnection()\n        Dim ServerName As String\n        Dim ServerNameRaw As String\n        Dim CubeName As String\n        Dim CubeNameRaw As String\n        Dim ConnectionString As String\n\n        ServerNameRaw = ActiveWorkbook.SlicerCaches("Slicer_ServerName").VisibleSlicerItemsList(1)\n        ServerName = Replace(Split(ServerNameRaw, "[")(3), "]", "")\n\n        CubeNameRaw = ActiveWorkbook.SlicerCaches("Slicer_CubeName").VisibleSlicerItemsList(1)\n        CubeName = Replace(Split(CubeNameRaw, "[")(3), "]", "")\n\n        If CubeName = "All" Or ServerName = "All" Then\n            MsgBox "Please Select One Cube and Server Name", vbOKOnly, "Slicer Info"\n        Else\n            ConnectionString = GetConnectionString(ServerName, CubeName)\n            UpdateAllQueryTableConnections ConnectionString, CubeName\n        End If\n    End Sub\n\n    Function GetConnectionString(ServerName As String, CubeName As String)\n        Dim result As String\n        result = "OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" &amp; CubeName &amp; ";Data Source=" &amp; ServerName &amp; ";MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2"\n        \'"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" &amp; CubeName &amp; ";Data Source=" &amp; ServerName &amp; ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False"\n        GetConnectionString = result\n    End Function\n\n    Function GetConnectionString(ServerName As String, CubeName As String)\n    Dim result As String\n    result = "OLEDB;Provider=MSOLAP.5;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" &amp; CubeName &amp; ";Data Source=" &amp; ServerName &amp; ";MDX Compatibility=1;Safety Options=2;MDX Missing Member Mode=Error;Update Isolation Level=2"\n    GetConnectionString = result\nEnd Function\n\nSub UpdateAllQueryTableConnections(ConnectionString As String, CubeName As String)\n    Dim cn As WorkbookConnection\n    Dim oledbCn As OLEDBConnection\n    Dim Count As Integer, i As Integer\n    Dim DBName As String\n    DBName = "Initial Catalog=" + CubeName\n\n    Count = 0\n    For Each cn In ThisWorkbook.Connections\n        If cn.Name = "ThisWorkbookDataModel" Then\n            Exit For\n        End If\n\n        oTmp = Split(cn.OLEDBConnection.Connection, ";")\n        For i = 0 To UBound(oTmp) - 1\n            If InStr(1, oTmp(i), DBName, vbTextCompare) = 1 Then\n                Set oledbCn = cn.OLEDBConnection\n                oledbCn.SavePassword = True\n                oledbCn.Connection = ConnectionString\n                oledbCn.Refresh\n                Count = Count + 1\n            End If\n        Next\n    Next\n\n    If Count = 0 Then\n         MsgBox "Nothing to update", vbOKOnly, "Update Connection"\n    ElseIf Count &gt; 0 Then\n        MsgBox "Update &amp; Refresh Connection Successfully", vbOKOnly, "Update Connection"\n    End If\nEnd Sub\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70947', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8418/']