VBA - 如何隐藏枢轴字段?

时间:2017-12-01 10:45:06

标签: vba excel-vba excel

我想使用下面的代码删除我的数据透视表中的某个字段,但它不起作用。这段代码有什么问题?

Dim pt As PivotTable
Dim pf As PivotField
Dim df As PivotField
Dim rows As Variant

Set pt = ActiveSheet.PivotTables(1)

'Remove rows not needed
rows = Array("A", "B", "C", "D")

For Each pt In ActiveSheet.PivotTables
   pt.PivotFields(rows(0)).Orientation = xlHidden
   pt.PivotFields(rows(1)).Orientation = xlHidden
   pt.PivotFields(rows(2)).Orientation = xlHidden
   pt.PivotFields(rows(3)).Orientation = xlHidden
Next pt

1 个答案:

答案 0 :(得分:2)

见内部评论:

Dim pt As PivotTable
Dim pf As PivotField
Dim df As PivotField
Dim PTrows As Variant '<< DO NOT USE ROWS WHICH IS RESERVED OBJECT INSTRUCTION

'Set pt = ActiveSheet.PivotTables(1) << YOU DON'T NEED IT IF YOU HAVE A LOOP

'Remove rows not needed
PTrows = Array("A", "B", "C", "D")

For Each pt In ActiveSheet.PivotTables
   pt.PivotFields(PTrows(0)).Orientation = xlHidden
   pt.PivotFields(PTrows(1)).Orientation = xlHidden
   pt.PivotFields(PTrows(2)).Orientation = xlHidden
   pt.PivotFields(PTrows(3)).Orientation = xlHidden
Next pt