【问题标题】:Sort all pivot tables in a workbook using VBA Excel使用 VBA Excel 对工作簿中的所有数据透视表进行排序
【发布时间】:2015-01-09 23:14:44
【问题描述】:

我正在尝试使用以下代码对许多工作表中的所有数据透视表进行排序:

Sub sortpivots()

    ActiveWorkbook.PivotTables().PivotFields("name").AutoSort _
       xlAscending, "Min of start", ActiveSheet.PivotTables(). _
       PivotColumnAxis.PivotLines(1)



End Sub

我制作了一个宏,但它又长又丑,必须有一种方法可以同时定位所有数据透视表。我哪里错了?

【问题讨论】:

  • 使用 For Each 浏览数据透视表集合。当然,你不能像那样一次性引用所有内容。

标签: excel vba sorting pivot-table


【解决方案1】:

您可以使用 For Each 循环,第一个循环遍历 Activeworkbook 的工作表,第二个循环遍历每个工作表中的数据透视表:

Sub sortpivots()
Dim ws As Excel.Worksheet
Dim pvt As Excel.PivotTable

For Each ws In ActiveWorkbook.Worksheets
    For Each pvt In ws.PivotTables
        ActiveWorkbook.PivotTables().PivotFields("name").AutoSort _
            xlAscending, "Min of start", ActiveSheet.PivotTables(). _
            PivotColumnAxis.PivotLines(1)
    Next pvt
Next ws
End Sub

【讨论】:

  • 这是正确的答案,但是我在 foreach 循环中的代码是错误的。我最终手动对数据透视表进行排序并放入一个刷新按钮,用数据透视表刷新工作表。感谢您的意见。
猜你喜欢
  • 2015-06-08
  • 2014-09-04
  • 2013-08-22
  • 2010-09-09
  • 2015-02-18
  • 2016-09-09
  • 2018-08-25
  • 1970-01-01
  • 2016-03-30
相关资源
最近更新 更多