update pivot table populated by recordset - adodb

cloudywaw

New Member
Joined
May 25, 2011
Messages
2
Hi!

If I use the procedure shown in this post (copy below, procedure name "createPivotTable") to create a recordset from a worksheet or a named range, and then use the recordset to create and then populate a pivot table, what will the correct procedure be to update the recordset and then to update the pivot table if:

1. values in the existing data source change,
2. if the number of records in the existing data source changes,
3. if the structure of the existing data source changes (add, take away columns/fields)
4. if I need to add a second or third worksheet or named range to the data source?

I tried the procedure below (name "refreshPivotTable") but it does not always work, specifically:

1. if I add an additional worksheet or a named range to the data source, then the update procedure fails (the procedure does not recognise the name of the new data range). I then need to save the file, close Excel, reopen the file and rerun the procedure, at which time the new data range is taken into consideration and the pivot table refreshes correctly,
2. changes to the structure of the data source do not show after the pivot table is refreshed, i.e. new fields do not show in the pivot table, or they do show but the values which belong to this field do not show in the pivot table.

Therefore, is there a better way to make the pivot table update as my source data changes? Am I doing this the wrong way?

Any tip will be greatly appreciated.

Tom



Sub createPivotTable()
With ActiveWorkbook
arSQL = "SELECT * FROM [03-11$]"
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open arSQL, _
Join$(Array("Provider=Microsoft.jet.OLEDB.4.0; Data Source=", _
.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)
End With
Set objPivotCache = .PivotCaches.Add(xlExternal)
Set objPivotCache.Recordset = objRS
Set objRS = Nothing
objPivotCache.CreatePivotTable TableDestination:=Sheet1.Range("A3")
Set objPivotCache = Nothing
End Sub


Sub refreshPivotTable()
With ActiveWorkbook
arSQL = "SELECT * FROM [03-11$] UNION ALL SELECT * FROM [04-11$]"
Set objRS = CreateObject("ADODB.Recordset")
objRS.Open arSQL, _
Join$(Array("Provider=Microsoft.jet.OLEDB.4.0; Data Source=", _
.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)
End With
Set ThisWorkbook.PivotCaches(1).Recordset = objRS
Sheet1.PivotTables(1).PivotCache.Refresh
Set objRS = Nothing
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top