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)
12840

在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
    移動訪問