使用变量数据透视表和数据透视字段名称
Using variable PivotTable and PivotField names
我正在尝试离开 "Name Hard Coding" 并进入 "Variable Coding",这样我就可以在其他数据中心使用我的代码,而不管我的数据源中有哪些字段名称。我想知道下面的示例代码有什么问题...
我收到以下错误:
运行-时间错误“1004”:
无法获取工作表 class
的数据透视表 属性
我正在使用 Microsoft Office 2013 中的“Excel 二进制工作簿 (*.xlsb)
例如:
名称硬编码:
With ActiveSheet.PivotTables("PivotTable10").PivotFields("Item")
这个效果很美!
可变编码:
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField)
这个不行!
示例代码:
Sub VariablePivotName_and_PivotField()
Dim pvtTable As PivotTable, pvtField As PivotField
For Each pvtTable In ActiveSheet.PivotTables
For Each pvtField In pvtTable.PivotFields
' MsgBox pvtTable.Name
' MsgBox pvtField.Name
' With ActiveSheet.PivotTables("PivotTable10").PivotFields("Item") '''' This one works like beauty!
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField) ''''This one does not work!
.Orientation = xlRowField 'Could be any other thing I want to change
.LayoutForm = xlTabular 'Could be any other thing I want to change
End With
Next
Next
End Sub
您遇到的问题是您试图将变量 pvtTable 和 pvtField 用作字符串,而不是它们本身的对象类型。
行数:
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField)
.Orientation = xlRowField
正在尝试获取名称为字符串 pvtTable 的数据透视表,然后获取名称为字符串 pvtField 的数据透视字段,但不幸的是,这些变量都不是字符串。
可行的行是:
With ActiveSheet.PivotTables(pvtTable.Name).PivotFields( pvtField.Name)
.Orientation = xlRowField
End With
因为 pvtTable.Name 是数据透视表的 属性,它是一个字符串。同样,pvtField.Name 是作为字符串的 pivotField 的 属性。
直接使用代码会更简单:
With pvtField
.Orientation = xlRowField
.LayoutForm = xlTabular
End With
因为这将作用于 PivotField 对象的字段。
我正在尝试离开 "Name Hard Coding" 并进入 "Variable Coding",这样我就可以在其他数据中心使用我的代码,而不管我的数据源中有哪些字段名称。我想知道下面的示例代码有什么问题...
我收到以下错误: 运行-时间错误“1004”: 无法获取工作表 class
的数据透视表 属性我正在使用 Microsoft Office 2013 中的“Excel 二进制工作簿 (*.xlsb)
例如:
名称硬编码:
With ActiveSheet.PivotTables("PivotTable10").PivotFields("Item")
这个效果很美!
可变编码:
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField)
这个不行!
示例代码:
Sub VariablePivotName_and_PivotField()
Dim pvtTable As PivotTable, pvtField As PivotField
For Each pvtTable In ActiveSheet.PivotTables
For Each pvtField In pvtTable.PivotFields
' MsgBox pvtTable.Name
' MsgBox pvtField.Name
' With ActiveSheet.PivotTables("PivotTable10").PivotFields("Item") '''' This one works like beauty!
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField) ''''This one does not work!
.Orientation = xlRowField 'Could be any other thing I want to change
.LayoutForm = xlTabular 'Could be any other thing I want to change
End With
Next
Next
End Sub
您遇到的问题是您试图将变量 pvtTable 和 pvtField 用作字符串,而不是它们本身的对象类型。
行数:
With ActiveSheet.PivotTables(pvtTable).PivotFields(pvtField)
.Orientation = xlRowField
正在尝试获取名称为字符串 pvtTable 的数据透视表,然后获取名称为字符串 pvtField 的数据透视字段,但不幸的是,这些变量都不是字符串。
可行的行是:
With ActiveSheet.PivotTables(pvtTable.Name).PivotFields( pvtField.Name)
.Orientation = xlRowField
End With
因为 pvtTable.Name 是数据透视表的 属性,它是一个字符串。同样,pvtField.Name 是作为字符串的 pivotField 的 属性。
直接使用代码会更简单:
With pvtField
.Orientation = xlRowField
.LayoutForm = xlTabular
End With
因为这将作用于 PivotField 对象的字段。