1

I have a pivot that I want to NOT re-sort after changing a filter. This would replicate in code the effect of right clicking the sorted field's drop-down arrow and going to More Options and unchecking the box for "Sort automatically every time the report is updated". I tried the macro recorder, but it didn't show anything.

I am sorting the pivot off a total including all options. I then want the order of my Rows to stay the same as I exclude/include certain options. Setting PivotFields.Autosort=xlManual just seemed to turn off and undo the sorting I do want. The line of code below is how I currently am sorting it. This works, I just want it to not keep autosorting.

PivotTables("PivotTable1").PivotFields("Category").AutoSort xlDescending, "Count of Category"

Example: If my original table is this when I filter for "A": enter image description here

When I change the filter to now filter for "B", I want the Categories (rows in the pivot) to stay in the same order, and not re-sort automatically by the Total. So it would look like this: enter image description here

1 Answer 1

2

You just need to disable automatic sorting, or in VBA terms, set it to manual:

Sub SortPivotWithoutAutoResorting()
    Dim pt As PivotTable
    Set pt = ActiveSheet.PivotTables("PivotTable1")

    'Automatically sort the pivot table
    pt.PivotFields("Category").AutoSort xlDescending, "Count of Category"
    
    'Turn off auto-sorting for future updates
    pt.PivotFields("Category").AutoSort xlManual, "Count of Category"
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

When I implement that code, turning the AutoSort to Manual causes the pivot to lose the Descending sort on Count of Category, and it sorts instead on the Rows (Categories) alphabetically. Is there something else I am missing? I'd like to preserve the order of the pivot rows from the Descending sort through any pivot changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.