设置pivotcache时类型不匹配

Roh*_*han 3 excel vba pivot-table

我有 pCach 作为 PivotCache

当我做

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

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

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

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

Sha*_*ado 5

尝试将PivotCache和拆分设置PivotTable为 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")
Run Code Online (Sandbox Code Playgroud)