繁体   English   中英

Excel VBA或VSTO-如何在数据透视表上的字段之间循环?

[英]Excel VBA or VSTO - How do you loop over Fields on a PivotTable?

这是一些示例VBA代码:

Sub Macro1()
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables("SomePivotTable")
    'Set colOfFields = pt.PivotFields  
End Sub

第三行不完整/损坏。 什么是访问数据透视表中所有字段的集合的正确方法? 我需要能够遍历它们。 实际编码在C#VSTO项目中完成。

这对我有用(Excel 2003 [11.8146.8202] SP2):

Sub Macro1()
    Dim pt As PivotTable
    Dim col As PivotFields
    Dim c As PivotField

    ' Name of the pivot table comes from right clicking on the pivot table,
    ' Table Options..., Name field.
    Set pt = ActiveSheet.PivotTables("PivotTable1")
    Set col = pt.PivotFields
    For Each c In col
        Debug.Print c.Name
    Next
End Sub

好。 从以下位置找到一些C#风格的代码构想:
http://blogs.msdn.com/andreww/archive/2008/07/25/creating-a-pivottable-programmatically.aspx

// pvtTable is an Excel.PivotTable set earlier in the code
Excel.PivotFields pflds =     
    (Excel.PivotFields)pvtTable.PivotFields(System.Type.Missing);
    foreach (Excel.PivotField pf in pflds)
    {
      //some code here
    }

技巧是传递System.Type.Missing来获取字段的“集合”。

暂无
暂无

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

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