2008-12-04 23 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

相關問題