2

这是一些示例 VBA 代码:

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

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

4

2 回答 2

3

这对我有用(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
于 2008-12-04T17:45:19.840 回答
1

行。从http://blogs.msdn.com/andreww/archive/2008/07/25/creating-a-pivottable-programmatically.aspx找到了一些 C# 风格的代码创意

// 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 以获取字段的“集合”。

于 2008-12-04T18:21:58.313 回答