【发布时间】:2015-07-06 08:36:08
【问题描述】:
我正在尝试自动尝试更改 Excel 中数据透视表的“数据源”。如果不是 Google/Stack Overflow 上提供的所有解决方案,我已经尝试过大部分。但是我一直遇到以下错误。
Run-time error '5':
Invalid procedure call or argument
这是我运行失败的代码。
Sub UpdateResourcePivots()
Dim pivotDataSheet As Worksheet
Dim dataSheet As Worksheet
Dim pivotLocation As String
Dim pivotDesignation As String
Dim startPoint As Range
Dim endPoint As Range
Dim dataRange As Range
Dim newRange As String
Dim lastRow As Integer
Dim newPivotCache As PivotCache
Set pivotDataSheet = ThisWorkbook.Worksheets("PivotData")
Set dataSheet = ThisWorkbook.Worksheets("Team Information")
pivotLocation = "pvtResourceLocation"
pivotDesignation = "pvtResourceDesignation" 'Dont worry about this one for now
Set startPoint = dataSheet.Range("A5")
lastRow = dataSheet.Range("A" & dataSheet.Rows.count).End(xlUp).row
Set endPoint = dataSheet.Range("O" & lastRow)
Set dataRange = dataSheet.Range(startPoint, endPoint)
newRange = "'" & dataSheet.name & "'!" & dataRange.Address(RowAbsolute:=True, ColumnAbsolute:=True, ReferenceStyle:=xlR1C1, External:=True)
' newRange = "'" & dataSheet.name & "'!" & dataRange.Address(ReferenceStyle:=xlR1C1) ' Tried this as well
Set newPivotCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=newRange, Version:=xlPivotTableVersion10)
With pivotDataSheet
.PivotTables(pivotLocation).ChangePivotCache newPivotCache 'Error thrown here.
End With
End Sub
以下行抛出错误:
.PivotTables(pivotLocation).ChangePivotCache newPivotCache
其他一些信息
我正在使用 MS Office 2010(许可)。 我有一个数据源(一个有 20 行和 8 列的小表;行会因此而增长)。在另一张纸上创建了两个数据透视表。在第三张纸上,我使用切片器过滤数据(仪表板)。
【问题讨论】:
标签: vba excel excel-2010