我对以下代码有疑问:
Sub sChangePivotRange()
Dim lngLastRow as Long
Dim rngPivotRange as Range
Dim strPivotRange as String
lngLastRow = ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row
Set rngPivotRange = ActiveSheet.Range("C5:AC" & lngLastRow)
strPivotRange = ActiveWorkbook.Path & "\[" & ActiveWorkbook.Name & "]" & ActiveSheet.Name & "!" & rngPivotRange.Address(ReferenceStyle:=xlR1C1)
ActiveSheet.PivotTables("RANKING").ChangePivotCache ActiveWorkbook.PivotCaches. _
Create(SourceType:=xlDatabase, SourceData:= _
"C:\[Ranks.xlsb]Ranks!R5C3:R117708C29" _
, Version:=6)
ActiveSheet.PivotTables("RANKING").ChangePivotCache ActiveWorkbook.PivotCaches. _
Create(SourceType:=xlDatabase, SourceData:= _
strPivotRange _
, Version:=6)
End Sub
这部分完美无缺:
ActiveSheet.PivotTables("RANKING").ChangePivotCache ActiveWorkbook.PivotCaches. _
Create(SourceType:=xlDatabase, SourceData:= _
"C:\[Ranks.xlsb]Ranks!R5C3:R117708C29" _
, Version:=6)
虽然这部分导致错误:
ActiveSheet.PivotTables("RANKING").ChangePivotCache ActiveWorkbook.PivotCaches. _
Create(SourceType:=xlDatabase, SourceData:= _
strPivotRange _
, Version:=6)
错误文本是:
“The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns. If you are changing the name of a PivotTable field, you must type a new name for the field.”
显然将?strPivotRange
立即窗口结果与C:\[Ranks.xlsb]Ranks!R5C3:R117708C29
.
提前感谢您帮助我。
卢卡斯