Hi,
I think I get crazy over creating Pivot tables with VBA. This is my unicorn (as they say in the movie "Gone in 60 seconds".
I have tried to find the answer in other posts but I cannot get it.
I get Run-time error '5': Invalid procedure call or argument.
I get the error code when I am here in the code
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
tabledestination:=StartPvt, \_
TableName:="PivotTable3")
This is my code:
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
' Make Pivot
'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & Range(Cells(VPivotDataRow, VPivotDataRow), Cells(VLastRow, VLastCol)).Address(ReferenceStyle:=xlR1C1)
Debug.Print "SrcData" & SrcData
Debug.Print "VPivotSheet" & VPivotSheet
'Create a new worksheet
Set sht = Sheets.Add
ActiveSheet.Name = VPivotSheet
'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)
Debug.Print "StartPvt" & StartPvt
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, \_
SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
tabledestination:=StartPvt, \_
TableName:="PivotTable3")
From the Debug.print I have the following result.
SrcData: 1RS!R1C1:R1084C24
VPivotSheet: Purchase part
StartPvt: Purchase part!R3C1
Just to be sure I have put in the row
Range(Cells(VPivotDataRow, VPivotDataRow), Cells(VLastRow, VLastCol)).Select
Just above SrcData = ...
And on that selection opened a pivot table manually in excel.