office交流網(wǎng)--QQ交流群號及微信交流群

Access培訓(xùn)群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

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

微信交流群(請用微信掃碼)

        

Excel vba創(chuàng)建數(shù)據(jù)透視表

2021-02-20 08:00:00
tmtony8
原創(chuàng)
12839

在Excel表格中,做數(shù)據(jù)分析常用到的就是數(shù)據(jù)透視表了??梢园凑詹煌绞街匦屡帕蟹治鰯?shù)據(jù)。

在菜單中,可以直接插入數(shù)據(jù)透視表的功能,按提示選擇表字段分析統(tǒng)計

也可以通過代碼創(chuàng)建數(shù)據(jù)透視表,方便自動處理一些常用的計算。


創(chuàng)建數(shù)據(jù)透視表代碼,在新的表格中創(chuàng)建一個數(shù)據(jù)透視表,在“A3”中開始顯示表數(shù)據(jù),設(shè)置字段組合的位置

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"))  '    創(chuàng)建數(shù)據(jù)透視表 
    With PT
        .PivotFields("Sex").Orientation = xlPageField
        .PivotFields("Month").Orientation = xlColumnField
        .PivotFields("Name").Orientation = xlRowField
        .PivotFields("Fa").Orientation = xlDataField 
        .DisplayFieldCaptions = False   '是否顯示字段標(biāo)題
    End With
End Sub

創(chuàng)建成功。

    分享
    文章分類
    聯(lián)系我們
    聯(lián)系人: 王先生
    Email: 18449932@qq.com
    QQ: 18449932
    微博: officecn01
    移動訪問