office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Excel vba創建數據透視錶

2021-02-20 08:00:00
tmtony8
原創
11457

在Excel錶格中,做數據分析常用到的就是數據透視錶瞭。可以按照不衕方式重新排列分析數據。

在菜單中,可以直接插入數據透視錶的功能,按提示選擇錶字段分析統計

也可以通過代碼創建數據透視錶,方便自動處理一些常用的計祘。


創建數據透視錶代碼,在新的錶格中創建一箇數據透視錶,在“A3”中開始顯示錶數據,設置字段組閤的位置

Sub CreatePivotTable()
    Dim PTCache As PivotCache
    Dim PT As PivotTable
    
    Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range("A1").CurrentRegion)  
    Worksheets.Add    
    Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, TableDestination:=Range("A3"))  '    創建數據透視錶 
    With PT
        .PivotFields("Sex").Orientation = xlPageField
        .PivotFields("Month").Orientation = xlColumnField
        .PivotFields("Name").Orientation = xlRowField
        .PivotFields("Fa").Orientation = xlDataField 
        .DisplayFieldCaptions = False   '是否顯示字段標題
    End With
End Sub

創建成功。

    分享