简体   繁体   English

Excel 2010 VBA循环数据透视表

[英]excel 2010 vba loop pivot tables

I have 16 pivot tables that I need to update when the user presses a button. 我有16个数据透视表,用户按下按钮时需要更新。 The code that needs to run is as follows: 需要运行的代码如下:

ActiveSheet.PivotTables("PivotTable10").PivotFields("count").ClearAllFilters
        ActiveSheet.PivotTables("PivotTable10").PivotFields("scrap code").ClearAllFilters
               ActiveSheet.PivotTables("PivotTable10").PivotFields("count").ShowAllItems = True

        With ActiveSheet.PivotTables("PivotTable10").PivotFields("count")
            .PivotItems("0").Visible = False
        End With

The names of the pivot tables are: MSP,MSP30,FSP,FSP30,MRP,MRP30,FRP,FRP30,MPP,MPP30,FPP,FPP30,MCP,MCP30,FCP,FCP30 数据透视表的名称为: MSP,MSP30,FSP,FSP30,MRP,MRP30,FRP,FRP30,MPP,MPP30,FPP,FPP30,MCP,MCP30,FCP,FCP30

I'd like to replace PivotTable10 with a variable that loops thru that list of pivot tables. 我想将PivotTable10替换为一个遍历该PivotTable10表列表的变量。 Right now my code is 16 blocks of the code above. 现在,我的代码是上面代码的16个块。 I know little about loops and I haven't found a good example of this type of loop in my google searches. 我对循环知之甚少,但在我的Google搜索中却找不到这种循环的好例子。

EDIT: finally code that worked, both answers below worked perfectly, this code just worked slightly faster 编辑:最终代码工作了,下面的两个答案都完美地工作,此代码只是稍快一点

Sub IteratePivots()

Application.ScreenUpdating = False

On Error Resume Next

Worksheets("Analytics Admin").Activate

Dim pvtTables As PivotTables
Dim pvtTable As PivotTable
Set pvtTables = Application.ActiveSheet.PivotTables
For Each pvtTable In pvtTables
    pvtTable.PivotFields("count").ClearAllFilters
    pvtTable.PivotFields("scrap code").ClearAllFilters
    pvtTable.PivotFields("count").ShowAllItems = True

    With pvtTable.PivotFields("count")
        .PivotItems("0").Visible = False
    End With
Next

Application.ScreenUpdating = False
End Sub

Try this code. 试试这个代码。 This code will iterates through all the pivotable in your sheet. 此代码将迭代工作表中的所有可旋转轴。

Sub IteratePivots()
Dim pvtTables As PivotTables
Dim pvtTable As PivotTable
Set pvtTables = Application.ActiveSheet.PivotTables
For Each pvtTable In pvtTables
    pvtTable.PivotFields("count").ClearAllFilters
    pvtTable.PivotFields("scrap code").ClearAllFilters
    pvtTable.PivotFields("count").ShowAllItems = True

    With pvtTable.PivotFields("count")
        .PivotItems("0").Visible = False
    End With
Next
End Sub

It's a little bit of a hack, but something like this might to the trick: 这有点骇人听闻,但这样的技巧可能会达到目的:

  Dim pt As PivotTable
  Dim ws As Worksheet
  Dim names As Variant
  Dim found As Boolean

  Set ws = ActiveWorkbook.ActiveSheet
  names = Split("MSP,MSP30,FSP,FSP30,MRP,MRP30,FRP,FRP30,MPP,MPP30,FPP,FPP30,MCP,MCP30,FCP,FCP30", ",")

  For Each pt In ws.PivotTables
    found = False

    For i = 1 To UBound(names)
      If pt.Name = names(i) Then
        found = True
      End If
    Next i

    If found Then
      pt.PivotFields("count").ClearAllFilters
      pt.PivotFields("scrap code").ClearAllFilters
      pt.PivotFields("count").ShowAllItems = True

      pt.PivotFields("count").PivotItems("0").Visible = False
    End If

  Next pt

You may notice my clumsy check to see if the name is in the array... This is a much better solution for that piece: 您可能会注意到我笨拙的检查,以查看名称是否在数组中……这是一个更好的解决方案:

https://stackoverflow.com/a/10952705/190829 https://stackoverflow.com/a/10952705/190829

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM