How to Create a Pivot Table in VBA

后端 未结 2 1574
自闭症患者
自闭症患者 2020-12-17 03:19

I\'m trying to create a Pivot table, but getting Invalid Procedure Call or Argument.

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, S         


        
相关标签:
2条回答
  • 2020-12-17 03:25

    In this instance, I used the wrong range object, which caused Excel to throw a fit.

    Set rng = wsA.Range("C14")
    Set rngData = Range(rng, rng.End(xlToRight))
    Set rngData = Range(rng, rng.End(xlDown))
    Set rngB = wsB.Range("C8")
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
    
    0 讨论(0)
  • 2020-12-17 03:34

    To create a pivot in Excel 2010, using VBA code, you can use and adapt this template:

    Sub newPVT()
        Dim PTCache As PivotCache
        Dim PT As PivotTable
    
        'Create the Cache
        Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:=Range("Dynamic_Field_Summary"))
    
        'Select the destination sheet
        Sheets("Field Summary").Select
    
        'Create the Pivot table
        Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, _
            TableDestination:=Range("P1"), TableName:="Pivot1")
    
        ActiveWorkbook.ShowPivotTableFieldList = True
    
        'Adding fields
        With PT
            With .PivotFields("Enterprise")
                .Orientation = xlColumnField
                .Position = 1
            End With
    
            With .PivotFields("Field")
                .Orientation = xlRowField
                .Position = 1
            End With
    
            With .PivotFields("Planted Acres")
                .Orientation = xlDataField
                .Position = 1
                .Caption = " Planted Acres"
                .Function = xlSum
            End With
    
            With .PivotFields("Harvested Acres")
                .Orientation = xlDataField
                .Position = 2
                .Caption = " Harvested Acres"
                .Function = xlSum
            End With
    
            With .PivotFields("lbs")
                .Orientation = xlDataField
                .Position = 3
                .Caption = " lbs"
                .Function = xlSum
            End With
    
            'Adjusting some settings
            .RowGrand = False
            .DisplayFieldCaptions = False
            .HasAutoFormat = False
    
            'Improving the layout
            .TableStyle2 = "PivotStyleMedium9"
            .ShowTableStyleRowStripes = True
            .ShowTableStyleColumnStripes = True
    
        End With
    
        With ActiveSheet
            'Adjusting columns width
            .Columns("P:V").ColumnWidth = 16
            .Range("Q2:V2").HorizontalAlignment = xlCenter
        End With
    
        ActiveWorkbook.ShowPivotTableFieldList = False
    End Sub
    

    I found it here.

    In this page you can also fine the meaning of every part of the code, for example is explained here. I think that this is a good code also to start creating vba macros for Excel 2007 or other versions.

    0 讨论(0)
提交回复
热议问题