{"id":1889,"date":"2007-01-25T14:11:51","date_gmt":"2007-01-25T13:11:51","guid":{"rendered":"https:\/\/yvespeneveyre8e29bf869d.wordpress.com\/2007\/01\/25\/automatically-refresh-pivottables-in-an-excel-workbook\/"},"modified":"2007-01-25T14:11:51","modified_gmt":"2007-01-25T13:11:51","slug":"automatically-refresh-pivottables-in-an-excel-workbook","status":"publish","type":"post","link":"https:\/\/www.peneveyre.com\/en\/2007\/01\/25\/automatically-refresh-pivottables-in-an-excel-workbook\/","title":{"rendered":"Automatically Refresh PivotTables in an Excel Workbook"},"content":{"rendered":"<p><P class=\"MsoNormal\"><FONT color=\"#000000\">In this project, I had to work on an Excel file getting its data from a database in order to generate a document usable for the end-user. The problem was that a lot of pivot tables were included everywhere in the document making the manual update of these tables really painful. So, my problem was to automate the process of this update in order to refresh all the pivot tables in all the worksheets. In finally wrote a little piece of code that could be triggered either manually or automatically after an event, for example.<\/FONT><\/P><br \/>\n<P class=\"MsoNormal\"><FONT color=\"#000000\">As it could be also useful for you, here is the VBA code :<\/P><br \/>\n<P><SPAN><SPAN>&#8216; Scans all the Pivot tables contained in the file and refresh the data<\/SPAN><BR><SPAN>Public<\/SPAN> <SPAN>Sub<\/SPAN> RefreshPivotTables()<BR><SPAN>&nbsp;&nbsp;&nbsp;Dim<\/SPAN> aWorksheet <SPAN>As<\/SPAN> Worksheet<BR><SPAN>&nbsp;&nbsp;&nbsp;Dim<\/SPAN> aPivotTable <SPAN>As<\/SPAN> PivotTable<BR><\/SPAN><\/P><br \/>\n<P><SPAN><SPAN>&nbsp;&nbsp;&nbsp;&#8216; Find the pivot tables in all worksheets<\/SPAN><BR><SPAN>&nbsp;&nbsp;&nbsp;For<\/SPAN> <SPAN>Each<\/SPAN> aWorksheet <SPAN>In<\/SPAN> Worksheets<BR><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216; If a worksheet contains more than one pivot table, refresh all of them<\/SPAN><BR><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For<\/SPAN> <SPAN>Each<\/SPAN> aPivotTable <SPAN>In<\/SPAN> aWorksheet.PivotTables<BR><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216; Updates also the range of data to be sure that the pivot table uses all the data<\/SPAN><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216; <FONT color=\"#008000\"><SPAN>If<\/SPAN> the range of data never changes, it <SPAN>is<\/SPAN> <SPAN>not<\/SPAN> necessary <SPAN>to<\/SPAN> <SPAN>call<\/SPAN> the <SPAN>sub<\/SPAN> below.<BR><\/FONT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RefreshPivotTableRange aPivotTable<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aPivotTable.PivotCache.Refresh<BR><SPAN>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<\/SPAN> aPivotTable<BR><SPAN>&nbsp;&nbsp;&nbsp;Next<\/SPAN> aWorksheet<BR><SPAN>End<\/SPAN> <SPAN>Sub<\/SPAN><BR><BR><SPAN>&#8216; Adapts a pivot table data range<\/SPAN><BR><SPAN>Private<\/SPAN> <SPAN>Sub<\/SPAN> RefreshPivotTableRange(aPivotTable <SPAN>As<\/SPAN> PivotTable)<BR><SPAN>On<\/SPAN> <SPAN>Error<\/SPAN> <SPAN>Resume<\/SPAN> <SPAN>Next<\/SPAN><BR><SPAN>&nbsp;&nbsp;&nbsp;Dim<\/SPAN> newRange <SPAN>As<\/SPAN> Range<BR><SPAN>&nbsp;&nbsp;&nbsp;Dim<\/SPAN> WorksheetName <SPAN>As<\/SPAN> <SPAN>String<\/SPAN><BR><\/SPAN><SPAN><SPAN><\/SPAN><\/SPAN><\/P><br \/>\n<P><SPAN><SPAN>&nbsp;&nbsp;&nbsp;&#8216; Get the worksheet name<\/SPAN><BR>&nbsp;&nbsp;&nbsp;WorksheetName <SPAN>=<\/SPAN> <SPAN>Left<\/SPAN>(aPivotTable.SourceData, <SPAN>InStr<\/SPAN>(aPivotTable.SourceData, <SPAN>&#8220;!&#8221;<\/SPAN>) <SPAN>&#8211;<\/SPAN> 1)<BR><FONT color=\"#008000\">&nbsp;&nbsp;&nbsp;&#8216;&nbsp;The GetDataRangeForWorksheet <SPAN><FONT color=\"#008000\">is<\/FONT><\/SPAN> a <SPAN><FONT color=\"#008000\">sub<\/FONT><\/SPAN> that returns the <SPAN><FONT color=\"#008000\">new<\/FONT><\/SPAN> range of data <SPAN><FONT color=\"#008000\">in<\/FONT><\/SPAN> the given worksheet.<BR><\/FONT><SPAN>&nbsp;&nbsp;&nbsp;Set<\/SPAN> newRange <SPAN>=<\/SPAN> GetDataRangeForWorksheet(Worksheets(WorksheetName))<BR>&nbsp;&nbsp;&nbsp;aPivotTable.SourceData <SPAN>=<\/SPAN> WorksheetName &amp; <SPAN>&#8220;!&#8221;<\/SPAN> &amp; newRange.Address(ReferenceStyle:=xlR1C1)<BR><SPAN>End<\/SPAN> <SPAN>Sub<\/SPAN><BR><\/P><\/SPAN><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to refresh automatically all the PivotTables contained in an Excel Workbook&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1889","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts\/1889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/comments?post=1889"}],"version-history":[{"count":0,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts\/1889\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/media?parent=1889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/categories?post=1889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/tags?post=1889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}