通过数据透视表循环并删除相同的值
我试图通过表单中的所有数据透视表来循环,并删除它们中所有具有相同名称的值字段:“Total Net Spend”和“%Split”(请参阅图片以供参考)。
我正在尝试下面的代码,但它只会在第一个枢轴工作,并不会遍历所有这些。 如何编辑代码,以便删除工作表中所有透视表上的“总净支出”和“%分解”列?
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
通过PivotTables
循环尝试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
试试下面的代码:
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