Is there a way of automating pivot tables using VBA?

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hello guys, I would like to create a two separate pivot tables automatically on a new sheet titled "Pivot". The excel workbook will have 3 sheets. One Named "Original" and one Named "Data". I would like to first create a pivot table based on the data in sheet "Original". Then create another pivot table based off the information in sheet "Data". These will be variable ranges.

Ideally it would look like this:

Book1
ABCDEF
1Order NumberDate PurchasedAmount + TaxAmount $DescriptionType
218/23/2022150100VideosN/A
328/23/2022200200HardwareN/A
438/23/2022300300SoftwareN/A
548/23/2022250250GamesN/A
658/23/2022300300AppliancesN/A
768/23/2022400400GamesN/A
878/23/20225050SoftwareN/A
988/23/20226060GamesN/A
10
11
12
13
14
15
16
17
Original



Book1
ABCDEFG
1Order NumberDate PurchasedAmount + TaxDescriptionType
218/23/2022150VideosN/A
328/23/2022200HardwareN/A
438/23/2022300SoftwareN/A
548/23/2022250GamesN/A
658/23/2022300AppliancesN/A
768/23/2022400GamesN/A
878/23/202250SoftwareN/A
988/23/202260GamesN/A
10
11
12
13
Data


Book1
ABCDEFG
1
2
3Row LabelsSum of Amount $Row LabelsSum of Amount + Tax
411001150
522002200
633003300
742504250
853005300
964006400
10750750
11860860
12Grand Total1660Grand Total1710
13
14
15
Pivot
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Here is a sample code for you. Run this code by debugging mode [F8] key would help your understanding.

VBA Code:
Sub Sample1()
    Dim pvtCache(1 To 2) As PivotCache
    Dim pvt(1 To 2) As PivotTable

    'Creating a worksheet named Pivot. If it already exists, delete it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Pivot").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Sheets.Add.Name = "Pivot"

    'Creating PivotCaches
    Set pvtCache(1) = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("original").Range("A1").CurrentRegion)

    Set pvtCache(2) = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("data").Range("A1").CurrentRegion)

    'Creating PivotTables (from PivotCaches)
    Set pvt(1) = pvtCache(1).CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
    Set pvt(2) = pvtCache(2).CreatePivotTable(TableDestination:=Sheets("Pivot").Range("F3"))

    'Setting fields
    With pvt(1)
        .PivotFields("Order Number").Orientation = xlRowField    'row field
        .PivotFields("Amount $").Orientation = xlDataField    'data field
    End With

    With pvt(2)
        .PivotFields("Order Number").Orientation = xlRowField    'row field
        .PivotFields("Amount + Tax").Orientation = xlDataField    'data field
    End With
End Sub
 
Upvote 0
Solution
Hi,

Here is a sample code for you. Run this code by debugging mode [F8] key would help your understanding.

VBA Code:
Sub Sample1()
    Dim pvtCache(1 To 2) As PivotCache
    Dim pvt(1 To 2) As PivotTable

    'Creating a worksheet named Pivot. If it already exists, delete it.
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Pivot").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Sheets.Add.Name = "Pivot"

    'Creating PivotCaches
    Set pvtCache(1) = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("original").Range("A1").CurrentRegion)

    Set pvtCache(2) = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("data").Range("A1").CurrentRegion)

    'Creating PivotTables (from PivotCaches)
    Set pvt(1) = pvtCache(1).CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
    Set pvt(2) = pvtCache(2).CreatePivotTable(TableDestination:=Sheets("Pivot").Range("F3"))

    'Setting fields
    With pvt(1)
        .PivotFields("Order Number").Orientation = xlRowField    'row field
        .PivotFields("Amount $").Orientation = xlDataField    'data field
    End With

    With pvt(2)
        .PivotFields("Order Number").Orientation = xlRowField    'row field
        .PivotFields("Amount + Tax").Orientation = xlDataField    'data field
    End With
End Sub
Thank you very much. This works perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top