簡體   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