Changing Worksheet & Pivot Table

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!
LVL 13
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Rory ArchibaldConnect With a Mentor Commented:
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") = "Ranges"

Open in new window

and then refresh the table.
Rory ArchibaldCommented:
If you use a named range as the data source for the pivot table (see here: then you can simply update the name to point to the new data (either in code, or manually) and then refresh the table.

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:

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.

Shanan212Author Commented:

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?
Rory ArchibaldCommented:
It should work fine with either method. What specific error are you getting?
Shanan212Author Commented:
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'
Rory ArchibaldCommented:
Did you really mean to refer to two different sheets in that formula?
Shanan212Author Commented:
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


Which is understandable. So is there anyway to assign the data source of a pivot from VBA to keep it constant?
Shanan212Author Commented:
This works :)

Shanan212Author Commented:
Just a quick question,

Could you please explain why its


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

eg: A1:G232

Rory ArchibaldCommented:
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)
Shanan212Author Commented:
Ah! Thats a nifty way to get the table :)

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.