Solved

Changing Worksheet & Pivot Table

Posted on 2011-09-26
12
292 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

765 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