Hi all,
I an trying to create a function to sort a pivot table according to the data from a certain column. So far, so good.
Next, I want to automate this so it will step through and sort as many Row Label fields as may exist.
the following code almost does this, but is stepping through all the pivotfields contained in the pivotcache as far as I can tell.
[VBA]
Sub SortPivotData(myPivot As PivotTable)
'sort pivot by 2nd column (most recent year)
'in "Sum of Consumption" pivot range
Dim myfield As PivotField
'debug: for each is cycling through entire input data range columns
For Each myfield In myPivot.PivotFields
myfield.AutoSort xlDescending, "Sum of Consumption (kWh)", _
myPivot.PivotColumnAxis.PivotLines(2), 1
Next myfield
End Sub
[/VBA]
Can anyone tell me what to put in the for each loop to only sort visible row labels?
many thanks