2017-02-20 50 views
3

我想通过循环表中的所有数据透视表并删除它们中具有相同名称的所有值字段:“Total Net Spend”和“%Split”(请参见图片参考)。通过数据透视表循环并删除相同的值

enter image description here

我想下面的代码,但它只能通过他们都在第一枢轴工作,不会循环。如何编辑代码,以便它将删除工作表中所有透视表上的“总净支出”和“%分割”列?

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

要遍历PivotTables尝试另一个foreach环这样

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

只是尝试,但一个错误弹出说:无法设置PivotField类的方向属性... – Andrea

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 
+0

感谢您的帮助,但此代码没有任何反应......通过原始代码,我能够摆脱“总净支出“和”%分割“,但它不会循环通过其余的枢轴... – Andrea

+0

@Andrea奇怪,为我工作。它会返回一个错误吗? –

+0

没有错误。它只是不会做任何事情...... – Andrea

相关问题