2008-12-04 76 views
2

下面是一些示例VBA代码:Excel VBA或VSTO - 如何循环数据透视表上的字段?

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

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

回答

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

嗯。让我去VSTO-land测试一下。什么是数据类型是col? – BuddyJoe 2008-12-04 17:57:23

相关问题