Type mismatch when setting up pivotcache

醉酒当歌 提交于 2021-01-27 12:44:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!