Solved

Changing Worksheet & Pivot Table

Posted on 2011-09-26
12
298 Views
Last Modified: 2012-05-12
Hi there,

I have a worksheet that get recreated every time a macro is run. I also have a pivot table that is dependent on data from this 'macro generated' worksheet.

What I have noticed is that, every time the macro is run, a new worksheet is created (old one is deleted) with same attributes as old one (like column names, etc)

Now, on the pivot table (is located in different worksheet), the range shifts/changes every time a new worksheet is changed.

Is there anyway to assign the range of the pivot from VBA? Or keep the range constant regardless of things happening to source?

What I am trying to do is get data from pivot which refers back to the macro-generated table.

Is there any way I can reset the data range of the pivot and refresh it? Or is there better suggestions?

Appreciate your help!
0
Comment
Question by:Shanan212
[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
  • 6
  • 5
12 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36707841
If you use a named range as the data source for the pivot table (see here: http://www.contextures.com/xlPivot01.html) then you can simply update the name to point to the new data (either in code, or manually) and then refresh the table.

HTH
Rory
0
 
LVL 5

Expert Comment

by:GirardAndrew
ID: 36708290
I would assume that the number of rows from the newly created worksheet is undetermined but the columns are not.

Therefore, when creating the pivot table via VBA you need to use a function to determine the last row that has data in it. Try using the following:

Cells.SpecialCells(xlCellTypeLastCell).Row
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36708529
Rorya,

It (Excel 2007) wouldn't accept named range as a source?

I even tried creating a table with the data available and it still wouldn't accept that table name as its range  in pivot table

any input?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36708544
It should work fine with either method. What specific error are you getting?
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36709519
I am using Excel 2007.

I named the formula

=OFFSET(Data!$A$1,0,0,COUNTA('GL Summary'!$A:$A),7)
as Ranges

When I try to set data source for pivot table (new) as 'Ranges' it wouldn't accept! It says 'Data source reference not valid'
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36709547
Did you really mean to refer to two different sheets in that formula?
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36709577
Woops! That I corrected (but I think I copied the wrong one here)

When thats corrected, and when I run a macro that 'deletes' the 'GL Summary' sheet and re-populates it

The pivot table is not refreshing. It sais 'reference not valid'

The formula under 'name manager' is changed to this

=OFFSET(#REF!R1C1,0,0,COUNTA(#REF!C1),7)

Which is understandable. So is there anyway to assign the data source of a pivot from VBA to keep it constant?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36709632
The pivot data source will still point at the named range - you just need to reinstate that in your code. Although, if you are using code anyway, you don't really need a dynamic named range. You could just use something like:
Sheets("GL Summary").Range("A1").Currentregion.name = "Ranges"

Open in new window

and then refresh the table.
0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 36709672
This works :)

Thanks!
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36709682
Just a quick question,

Could you please explain why its

.Range("A1")

Does that means it takes the full range of that table starting from A1?

eg: A1:G232

Thanks
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36709714
Range("A1").Currentregion is the solid table of data surrounding A1. (the same as you would get if you selected A1 and then pressed Ctrl+A)
0
 
LVL 13

Author Comment

by:Shanan212
ID: 36709821
Ah! Thats a nifty way to get the table :)

Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

763 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