Solved

Changing Worksheet & Pivot Table

Posted on 2011-09-26
12
287 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
  • 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

778 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