在VBA中创建透视表时出现无效的过程调用或参数错误
我在excel VBA中记录了一个宏,该宏使用另一个表创建透视表。我不确定为什么会出现这样的错误:“运行时错误'5'”过程调用或参数无效 有人能告诉我我做错了什么吗在VBA中创建透视表时出现无效的过程调用或参数错误,vba,excel,Vba,Excel,我在excel VBA中记录了一个宏,该宏使用另一个表创建透视表。我不确定为什么会出现这样的错误:“运行时错误'5'”过程调用或参数无效 有人能告诉我我做错了什么吗 Sheets("Sheet1").Select Range("A1").Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlDown)).Select ActiveWorkbook.PivotCaches
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”中创建“PivotTable1”,以防它不存在。如果它已经创建(来自过去的代码运行),只需使用更新的
数据透视缓存更新它即可
代码
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
请尝试下面的代码在“Sheet6”中创建“PivotTable1”,以防它不存在。如果它已经创建(来自过去的代码运行),只需使用更新的数据透视缓存更新它即可
代码
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
创建数据透视表后,您不能将其他人也命名为“数据透视表1”。您应该相应地更改名称。也可以更改位置。但我猜位置不会触发错误,而是会导致警告。^另外,什么是“table\u FTE\u Distributions4.24”
?命名范围?表格(ListObject
)?这是我正在做轴的表的名称。@Michaela阅读我的答案并测试下面的代码,让我知道它是否适用于you@ShaiRado我重新命名了数据透视表,它成功了-谢谢你的帮助!一旦你创建了一个数据透视表,你就不能将其他人命名为“数据透视表1””“我也是。您应该相应地更改名称。也可能是这个位置。但我猜位置不会触发错误,而是会引起警告。^另外,什么是“Table\u FTE\u Distributions4.24”
<代码>命名范围
<代码>表格(列表对象
)?这是我正在做轴的表格的名称。@Michaela阅读我的答案并测试下面的代码,让我知道它是否适用于you@ShaiRado我重新命名了pivot表,它成功了-谢谢你的帮助!