Pivot Table Destination Problem

swayp11

Board Regular
Joined
Apr 27, 2009
Messages
107
Office Version
  1. 365
Platform
  1. Windows
I'm having an error when I specify the table destination.

Here is my code:

Code:
Worksheets.Add After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = "Pivot Table"

Worksheets("Pivot Table Data").Activate
Range("A1").CurrentRegion.Name = "PivotRange"

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:="=PivotRange").CreatePivotTable TableDestination:="Pivot Table!R3C1", TableName:="PivotTable"

When I have TableDestination="", the code works fine.

I'm confused because I'm actually creating that sheet in the code right before creating the pivot table.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I would just be cautious with how your refer to ranges and always use the worksheet references with the range names.

Code:
Add(SourceType:=xlDatabase, _
    [COLOR="Red"]SourceData:="=PivotRange"[/COLOR]
'//TRY INSTEAD:
Add(SourceType:=xlDatabase, _
    SourceData:=[COLOR="Blue"]Worksheets("Pivot Table Data").Range("PivotRange"))[/COLOR]
Same for:
Code:
TableDestination:="[COLOR="Red"]Pivot Table!R3C1[/COLOR]"
'//TRY INSTEAD:
TableDestination:=[COLOR="Blue"]Worksheets("Pivot Table").Range("A3")[/COLOR]

It's not that these other methods are wrong per se, but experiment with names and R1C1 notation some other time ;)

Code:
Sub Foo()

Worksheets.Add After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = "Pivot Table"

Worksheets("Pivot Table Data").Activate
Range("A1").CurrentRegion.Name = "PivotRange"

Set PC = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
    SourceData:=Worksheets("Pivot Table Data").Range("PivotRange"))
    
PC.CreatePivotTable _
    TableDestination:=Worksheets("Pivot Table").Range("A3"), _
        TableName:="PivotTable"

End Sub

For what its worth, do we really need to add a pivot table repeatedly? Can we have the pivot table in place and just refresh it when the new data arrives?

Regards,
Alex
 
Upvote 0
Note: Usually spaces in names require single quotes too but its odd that it worked the first time. Oh well - that is odd...
'Pivot Table'!R3C1
 
Upvote 0
Thank you Alexander.

To answer some of your questions/comments. The R1C1 reference was simply the last thing I tried. The code started to get ugly cause I couldn't get it to work, so I was trying every way I can think of (obviously to no avail).

The Pivot Table won't be added repeatedly. It is mostly an experiment, and is being built off of some commands from a userform.

Thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,216,741
Messages
6,132,448
Members
449,728
Latest member
teodora bocarski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top