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
493 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

696 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