设置pivotcache时键入不匹配

时间:2017-01-11 12:34:16

标签: vba excel-vba pivot-table excel

我有pCach作为PivotCache

当我做的时候

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

它按预期工作,并在目标单元格上插入pivotcache

但是当我尝试这个时它会给我类型不匹配错误?

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

1 个答案:

答案 0 :(得分:4)

尝试拆分将PivotCachePivotTable设置为2个单独的代码行,如下面的代码所示:

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")