Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
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
Medium Priority
?
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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:Tracy
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

647 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