我正在尝试遍历工作表中的所有数据透视表,并删除其中具有相同名称的所有值字段:“总净支出”和“%拆分”(参见图片)。
我正在尝试下面的代码,但它只能在第一个数据透视图上运行,并且不会遍历所有代码。如何编辑代码,以便删除工作表中所有数据透视表上的“总净支出”和“%拆分”列?
Sub Loop_Pivots()
Dim PT As PivotTable, PTField As PivotField
Set PT = Sheets("Sheet1").PivotTables("Pivot1")
With PT
.ManualUpdate = True
For Each PTField In .DataFields
PTField.Orientation = xlHidden
Next PTField
.ManualUpdate = False
End With
Set PT = Nothing
End Sub
答案 0 :(得分:0)
要循环PivotTables
尝试另一个for
each
这样的循环
Sub Loop_Pivots()
Dim PT As PivotTable, PTField As PivotField
For Each PT In Sheets("Sheet1").PivotTables
With PT
.ManualUpdate = True
For Each PTField In .DataFields
PTField.Orientation = xlHidden
Next PTField
.ManualUpdate = False
End With
Next PT
Set PT = Nothing
End Sub
答案 1 :(得分:0)
尝试以下代码:
Option Explicit
Sub Loop_Pivots()
Dim PT As PivotTable
Dim PTField As PivotField
For Each PT In Sheets("Sheet1").PivotTables
With PT
.ManualUpdate = True
For Each PTField In .PivotFields '<-- loop through all pivot fields
Select Case PTField.Name
Case "Total Net Spend", "% Split" '<-- if Field Name equals on of the 2 in this case
PTField.Orientation = xlHidden
End Select
Next PTField
.ManualUpdate = False
End With
Set PT = Nothing
Next PT
End Sub