Solved

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

Posted on 2011-03-17
6
484 Views
Last Modified: 2012-05-11
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
Comment
Question by:peyton18
  • 2
  • 2
6 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 35158875
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
 

Author Comment

by:peyton18
ID: 35159091
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
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 35159242
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
 

Author Comment

by:peyton18
ID: 35160063
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
 
LVL 24

Expert Comment

by:broomee9
ID: 35821672
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now