Solved

Changing Worksheet & Pivot Table

Posted on 2011-09-26
12
269 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

758 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

19 Experts available now in Live!

Get 1:1 Help Now