Changing DataFields in Data Model using VBA

IrishChristof

New Member
Joined
Sep 17, 2018
Messages
8
Hi,

I'm trying to do a VBA script that will allow me to change what is shown on a pivot table I have build from my Data Model.
So when I change the value in cell AO5, it will change from displaying Sum of Pax (sorted largest to smallest), to Sum of Total Revenue (largest to smallest).

The script worked on a normal Pivot, but doesnt seem to work on a pivot from the Data Model.

Code:
Private Sub CommandButton1_Click()




        Dim pt1 As PivotTable
        Dim Field1 As PivotField
        Dim pi As PivotItem
        Dim sumField As String


        sumField = Range("AO5").Value


        '-----Pivot1-----




        Set pt1 = PivotTables("PivotTable1")




        pt1.DataFields(1).Orientation = xlHidden
        pt1.PivotFields(sumField).Orientation = xlDataField
        pt1.AllowMultipleFilters = True
        pt1.RefreshTable


        If sumField = "Total Revenue" Then


        ActiveSheet.PivotTables("PivotTable1").PivotFields("Main Tour Code").AutoSort _
        xlDescending, "Sum of Total Revenue", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(1), 1


        Else




        ActiveSheet.PivotTables("PivotTable1").PivotFields("Main Tour Code").AutoSort _
        xlDescending, "Sum of Pax", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(1), 1


        End If

End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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