Consulting

Results 1 to 5 of 5

Thread: Pivot table sort function

  1. #1
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    Pivot table sort function

    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
    Remember: it is the second mouse that gets the cheese.....

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Check out .Orientation
    myfield.orientation is 1 if it's a row field.
    xlRowfield is an excel built in constant equal to 1.

    so:
    if myfield.orientation = xlRowField then
    should process only those fields.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Or simply loop through the Rowfields
    [vba]For Each myfield In myPivot.RowFields[/vba]
    Be as you wish to seem

  4. #4
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    Thumbs up

    hmm, this sounds good. If I suddenly go very quiet then you know that I am being very grateful. I believe it is a rowfield, so this should work

    ps. I am related to Murphy......

    Thanks
    Remember: it is the second mouse that gets the cheese.....

  5. #5
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    Thumbs up Resolved: Advanced Sort in Pivot Table

    Many thanks to p45cal and Aflatoon - your suggestions worked, and your explanation has taught me something.

    for everyone else who is looking for this answer:
    Pivot tables allow you to sort a row label field according to a data field value. I had Sum of Consumption, year 1 and year 2, and wanted to sort by 'year 2' column only. The Pivot table has an Advanced Sort option that allows this, and this code cycles through as many Row Labels as may or may not exist and sets this sort order


    [vba]Sub SortPivotData(myPivot As PivotTable)
    'sort pivot by 2nd column (most recent year)
    'in "Sum of Consumption" pivot range

    Dim myField As PivotField

    For Each myField In myPivot.RowFields
    myfield.AutoSort xlDescending, "Sum of Consumption (kWh)", _
    myPivot.PivotColumnAxis.PivotLines(2), 1
    Next myField
    Set myField = nothing
    End Sub[/vba]
    Remember: it is the second mouse that gets the cheese.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •