VBA Excel Sort dynamically HELP

tryharder1997

New Member
Joined
Jan 2, 2019
Messages
2
Hi, I'm fairly new to writing code and trying to write a macro that will sort my pivot table by either a month, or by a subtotal for a group. The months are just grouping for days and these months also fall into another grouping done by adding another row field not grouping in the pivot table. The only issue is that sometimes the data in pivot table changes so using the pivotline(#) reference is hard.

Code:
Sub macro5()'


    Sheets("Pivot").Select
Dim score As Integer
score = Range("B1").Value
If score = "0" Then
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Sold to #").AutoSort _
        xlDescending, "YTD-2018-NOV"
Else
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Billed to #").AutoSort _
        xlDescending, "YTD-2018-NOV"
End If
    Sheets("Report").Select
End Sub
This is the pivot lines code, but would rather i referred to the name of a column sort of like in GetPivotData.
Code:
Sub Macro4()
'
' Macro4 Macro
'


'
    Sheets("Pivot").Select
Dim score As Integer
score = Range("B1").Value
If score = "0" Then
   Application.CutCopyMode = False
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Billed to #").AutoSort _
        xlDescending, "Sum of CUR", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(19), 1
Else
      Application.CutCopyMode = False
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Billed to #").AutoSort _
        xlDescending, "Sum of CUR", ActiveSheet.PivotTables("PivotTable1"). _
        PivotColumnAxis.PivotLines(20), 1
End If
    Sheets("Report").Select
End Sub

any help is much appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,224,216
Messages
6,177,189
Members
452,763
Latest member
WH12TTY

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