无效的过程调用或参数错误在VBA中创build数据透视表

我logging了一个macros创build一个数据透视表在Excel VBA中的另一个表。 我不知道为什么我得到这个错误:“运行时错误'5'”无效的过程调用或参数。

有人能告诉我我做错了什么吗?

Sheets("Sheet1").Select Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Table_FTE_Distributions4.24", Version:=xlPivotTableVersion15).CreatePivotTable TableDestination:= _ "'Sheet6'! R3C1", TableName:="PivotTable1", DefaultVersion:=15 

如果不存在,请尝试下面的代码在“Sheet6”中创build“PivotTable1”。 如果已经创build(从过去的代码运行),只需使用更新的PivotCache更新即可。

 Option Explicit Sub RefreshPivots() Dim ShtPivot As Worksheet Dim PivTbl As PivotTable Dim PivCache As PivotCache Dim Tbl As ListObject Dim SrcRng As Range ' set the Pivot Sheet Set ShtPivot = Worksheets("Sheet6") ' set the ListObject to "Table_FTE_Distributions4.24" Set Tbl = Worksheets("Sheet1").ListObjects("Table_FTE_Distributions4.24") ' set the Pivot Caches SourceData Set SrcRng = Tbl.Range ' set the Pivot Cache Set PivCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRng) ' add this line in case the Pivot table doesn't exit >> first time running this Macro On Error Resume Next Set PivTbl = ShtPivot.PivotTables("PivotTable1") ' check if "PivotTable1" Pivot Table already created (in past runs of this Macro) On Error GoTo 0 If PivTbl Is Nothing Then ' create a new Pivot Table in "Sheet6", start from Cell A3 Set PivTbl = ShtPivot.PivotTables.Add(PivotCache:=PivCache, TableDestination:=ShtPivot.Range("A3"), TableName:="PivotTable1") Else ' just refresh the Pivot cache with the updated Range in "Table_FTE_Distributions4.24" MsgBox "PivotTable1 already exists, onyl need to refresh the Pivot Cache" PivTbl.ChangePivotCache PivCache PivTbl.RefreshTable End If End Sub