VBA - creating a Pivot Table with Sum of Values showing on Columns instead of Rows

Hnajibeddine

New Member
Joined
Feb 1, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to create a pivot table using VBA such as the pivot row is based on Column "Product", and the columns in the pivot show the Sum of Column "Sales" and "Profit".

Below is an extract of the VBA code I'm using.

Set pvtField = pvtTable.PivotFields("Product")
pvtField.Orientation = xlRowField
pvtField.Position = 1

With pvtTable.PivotFields("Sales")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
End With

With pvtTable.PivotFields("Profit")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
End With

This code results in a pivot similar to the one in red in the picture below. I've also tried other ways ex, by using xlColumn instead of xlDataField but it did not help.
The end result I'm looking for is shown in Green.

Can someone recommend a way to create such a pivot in VBA? In the actual pivot table, if I simply move the Sigma Values from Rows to Columns (as shown by the blue arrow), it fixes the issue. But I'm unable to translate this into VBA.

Thanks,
Hassan

Capture.PNG
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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