Hnajibeddine
New Member
- Joined
- Feb 1, 2023
- Messages
- 1
- Office Version
- 365
- Platform
- 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
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