Solved

Changing Worksheet & Pivot Table

Posted on 2011-09-26
12
307 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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.

622 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