Link to home
Start Free TrialLog in
Avatar of peyton18
peyton18Flag for United States of America

asked on

How do I refresh a Pivot Table in Excel 2007 using VBA if the range of my data changes?

Hi,
I'm running a query on an external set of data which then, saves the recordset data on a worksheet in an Excel ("source").  I then use the data on the "source" worksheet to update a pivot table in the same workbook ("target").  Because I am obtaining the data from a query, the number of rows in the "source" changes almost every time I run the query.  The problem I am having is that when I try to refresh the pivot table, it is not collecting all of the data from the "source"; it is only using the old range to update the data, it is not expanding the range to include new data.
   I am using the following code (snippet) to refresh the pivot table:

dim pvt as PivotTable

  with pvt
     .RefreshTable
 end with

Can you tell me how I can get the pivot table to dynamically change the range of data it uses (using VBA)?

  Peyton18
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

You can name the range that the recordset goes into and use that range as the source for the pivot table. Then simply refreshing the table should work. Either that or use an external data query (that will automatically become a table and then if you use the table as the source for the pivot it works like a dynamic named range) or simply assign the recordset directly to the pivot table's pivot cache if you don't need the data stored in a sheet.
Avatar of peyton18

ASKER

Hi rorya,

Thanks for your help.  Unfortunately, I can't use the recordset directly.  I further manipulate it after it is downloaded to incorporate some additional filters that can't be incorporated into the query.  I'll try creating the pivot cache with the manipulated data.  Let's say that the manipulated data is on worksheet "pivotsource".  Would you happen to know the code I might use to do this?  Or would it be easier to create a named data range with the worksheet contents?
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi rorya,
Using the dynamic named range worked! The www.contextures.com website really helped.

To help others (and probably myself again) in the future, here is what I did:
  1)  I saved my manipulated data to another worksheet ("pivotsource") in the same workbook ("wkbk").
  2)  I created a dynamic named range for the data on "pivotsource".  (In Office 2007, go to "Formulas -> go to the "Defined Names" area and select "Define Name".  Enter name ("DynaRange"), then scope ("WORKBOOK"), then enter a formula for the dynamic range.  I used the following formula which captures data from cells A1:n17 where n is the # of rows with data in them. (I only needed data up through the 17th column):
  =OFFSET(pivotsource!$A$1,0,0,COUNTA(pivotsource!$A:$A),17)
3) Since my pivot table already exists and I am updating it (rather than creating it), I click on the "Pivot Table Tools" Menu and select "Change Data Source".  Select the "Change Data Source" option.  Select the "Select a table or range" option, and enter the name of the dynamic named range ("DynaRange"), and click <OK>.
4)  I ran my code after this, and all of my data was retrieved!

Obviously my workbook is comprised of both built-in functions and VBA code, so it is a little confusing, but thank you so much for your help!
  -Peyton18
 
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.