问题
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")
回答1:
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")
来源:https://stackoverflow.com/questions/41591111/type-mismatch-when-setting-up-pivotcache