Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

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
0
peyton18
Asked:
peyton18
  • 2
  • 2
1 Solution
 
Rory ArchibaldCommented:
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.
0
 
peyton18Business Systems AnalystAuthor Commented:
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?
0
 
Rory ArchibaldCommented:
It would be easier to name the range. You can set up a dynamic named range so that your code doesn't have to do anything other than refresh the table. If you have a look at the pivot table tips on www.contextures.com you will see what I mean.
0
 
peyton18Business Systems AnalystAuthor Commented:
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
 
0
 
TracyVBA DeveloperCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now