Consulting

Results 1 to 5 of 5

Thread: Pivots auto sort and dollar sign needs to be added

  1. #1

    Pivots auto sort and dollar sign needs to be added

    Hi all,

    I have this small code that creates a basic pivots with the source data available . everything is fine but I unable to " Sort " and assign a dollar sign to it . I tried various ways using online . so thought I could reach to place I get a advice .

    below is the code for your reference .
    Sub pivot_table_code2()
    With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Raw data").Range("A1").CurrentRegion).CreatePivotTable(TableDestination:=Sheets("Summary").Range("A4"))
    With .PivotFields("Credit analyst")
         .Orientation = xlRowField
         .Position = 1
    End With
    .AddDataField .PivotFields("Overdue Total"), "Sum of overdue total", xlSum
    .AddDataField .PivotFields("90+"), "Sum of 90+", xlSum
    End With
    With ActiveSheet.PivotFields("Credit analyst")
    .AutoSort Order:=xlDescending, Field:="90+"
    End With
    
    End Sub
    and attached is the file with my wish pivot I wanted to be using the code all the time.

    any inputs are appreciated.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. the sort order seems to be manual

    2. I like the PT wizard method since I think it's easier and clearer to use


    Option Explicit
    Sub Macro4()
        Dim pt As PivotTable
        
        'expression.PivotTableWizard( _
        '   SourceType, SourceData, TableDestination, TableName,
        '   RowGrand, ColumnGrand, SaveData, HasAutoFormat,
        '   AutoPage, Reserved,
        '   BackgroundQuery, OptimizeCache, PageFieldOrder, PageFieldWrapCount, ReadData, Connection)
        Set pt = Worksheets("Summary").PivotTableWizard( _
            xlDatabase, Sheets("Raw data").Range("A1").CurrentRegion, Worksheets("Summary").Range("A4"), "SummaryPT", _
            False, True, True, False)
        
        With pt
            With .PivotFields("Credit analyst")
                .Orientation = xlRowField
                .Position = 1
                .PivotItems("James").Position = 1
                .PivotItems("danny").Position = 2
                .PivotItems("ramu").Position = 3
                .PivotItems("Hudson").Position = 4
                .PivotItems("Jack").Position = 5
                .PivotItems("Maddy").Position = 6
            End With
        
            .AddDataField .PivotFields("Overdue Total"), "Sum of Overdue Total", xlSum
            .AddDataField .PivotFields("90+"), "Sum of 90+", xlSum
            
            .PivotFields("Data").Orientation = xlColumnField
            
            .PivotFields("Sum of Overdue Total").NumberFormat = "$#,##0.00_);([red]$#,##0.00)"
            
            .PivotFields("Sum of 90+").NumberFormat = "$#,##0.00_);([red]$#,##0.00)"
        
            .TableRange2.EntireColumn.AutoFit
        End With
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul,

    Thank you so much for your time. this is very good but this is not what I am looking for . besides my column (90+) is not sorted xldescending . can we make it with out wizard please .

    thanks you once again.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    If you use the macro recorder, you can get the syntax and then integrate it into your overall macro

    Sub pivot_table_code2()
    
        With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("Raw data").Range("A1").CurrentRegion).CreatePivotTable(TableDestination:=Sheets("Summary").Range("A4"))
    
            With .PivotFields("Credit analyst")
                 .Orientation = xlRowField
                .Position = 1
            End With
    
            .AddDataField .PivotFields("Overdue Total"), "Sum of overdue total", xlSum
            .AddDataField .PivotFields("90+"), "Sum of 90+", xlSum
        
            .PivotFields("Sum of Overdue Total").NumberFormat = "$#,##0.00_);([red]$#,##0.00)"
            .PivotFields("Sum of 90+").NumberFormat = "$#,##0.00_);([red]$#,##0.00)"
            
            With .PivotFields("Credit analyst")
                .AutoSort Order:=xlDescending, Field:="Sum of 90+"
            End With
        End With
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Hi Paul,

    Perfect , this is what i am looking for . thanks for your time mate .

    I really appreciate your effort .

Posting Permissions

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