VBA - Move Pivot Table data fields to the row values area to sum

Sheila8659

New Member
Joined
Mar 1, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I am in need of moving several PT data fields to the values field to sum. I have about 100 pivot tables in the workbook and need this to be looped through each of them. I only show one field in the example below, but the solution needs to show multiple data fields being moved i.e. "FABRIC", "L1", "L2" etc. I'm getting stuck on the with and end with statements. I don't know how to properly use or incorporate the adddatafield. I tried to use the principal without the with and end with statements, but haven't been able to get the code to process with that code as well. I've noticed that there are no answers pertaining to pivot table data fields on any forums. Is what I am asking a possibility? Or, can this not be achieved with VBA?

PT Data Fields: Image attached
I would like to move "FABRIC" to the values field as highlighted. As you can see "L1" and "L2" are showing in the example, but would need to be also be moved on the remaining sheets.


Sub MovePTFields()

Dim PT As PivotTable
Dim PF As PivotField

Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets

WS.Activate

If WS.Name <> "Factor" And WS.Name <> "Custom Dealer Price List" And WS.Name <> "Dealer Price List - Detail" And WS.Name <> "LINEUP" And WS.Name <> "Tabular - Price List" And WS.Name <> "TEMPLATE" And WS.Name <> "Standard - Option" Then

For Each PT In ActiveSheet.PivotTables

For Each PF In PT.DataFields

If PF.Name = "FABRIC" Then

' Move the field to the Rows Value area and sum the values
' PF.Position = 1
With PT

.AddDataField ActiveSheet.PF("FABRIC"), "Sum of FABRIC", xlSum
End With

'' PF.AddDataField = xlSum
'' PF.Position = 1
'' PF.Name = "FABRIC"

' PF.Orientation = XlPivotFieldOrientation.xlrowvalue
' PF.Function = xlSum

End If

Next PF

' PT.RefreshTable

Next PT

End If

Next WS

End Sub
 

Attachments

  • PT Data Fields.jpg
    PT Data Fields.jpg
    29.8 KB · Views: 17

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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