Sheila8659
New Member
- Joined
- Mar 1, 2022
- Messages
- 7
- Office Version
- 365
- Platform
- 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
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