Stupid_Intern
Stupid_Intern

Reputation: 3450

Type mismatch when setting up pivotcache

I have pCach as PivotCache

when I just do

ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pTRng).CreatePivotTable(TableDestination:= _
    wOPT.Cells(3, 1), TableName:="PivotTable2")

It works as expected and inserts pivotcache on the destination cell

But when I try this it gives me type mismatch error?

 Set pCach = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pTRng).CreatePivotTable(TableDestination:= _
    wOPT.Cells(3, 1), TableName:="PivotTable2")

Upvotes: 4

Views: 671

Answers (1)

Shai Rado
Shai Rado

Reputation: 33692

Try splitting setting the PivotCache and the PivotTable to 2 seperate code lines, like in the code below :

Dim pTbl  As PivotTable
Dim pCach As PivotCache

' set the Pivot Cache
Set pCach = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pTRng)

' create a new Pivot Table in "wOPT" sheet, start from Cell A3
Set pTbl = wOPT.PivotTables.Add(PivotCache:=pCach, TableDestination:=wOPT.Range("A3"), TableName:="PivotTable2")

Upvotes: 7

Related Questions