Trouble creating pivot table in VBA

jasonfish11

Board Regular
Joined
May 14, 2015
Messages
56
All,
I've searched for the answer to my question and haven't found much help. I was hoping someone could point out where I'm missing something as the below coding fails at the last line and never creates a pivot table.

Code:
'Create pivot tables
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String


Worksheets("Combined").Select
SrcData = ActiveSheet.Name & "!" & Range("A1:E" & comLR2).Address(ReferenceStyle:=xlR1C1)


Worksheets("Recon Pivot").Select
StartPvt = ActiveSheet.Name & "!" & Range("A2").Address(ReferenceStyle:=xlR1C1)
Set pvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData)
Set pvt = pvtCache.CreatePivotTable(TableDestination:=StartPvt, TableName:="PivotTable1")

The variables of SrcData and StartPvt both appear correctly when I hover over them in VBA.
Any help would be much appreciated.
 
Last edited:
So I did some more digging on the internet and found that I had to add apostrophe's in the StartPvt coding. I'm not sure why yet but it is now working.

StartPvt = "'" & ActiveSheet.Name & "'!" & Range("A2").Address(ReferenceStyle:=xlR1C1)

RoryA your coding might work as well, but I hadn't tried it since finding the above solution.

Thank you everyone for the help.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If the sheet name contains spaces, or looks like a date, or any of the other reasons that it would be enclosed in quotes in a formula string applies, you need to use them there. No real need for the Range("A2").Address(ReferenceStyle:=xlR1C1) when you could just use:

Code:
StartPvt = "'" & ActiveSheet.Name & "'!R2C1!
 
Upvote 0

Forum statistics

Threads
1,223,449
Messages
6,172,235
Members
452,449
Latest member
dglswt0519

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