Solved

VBA Excel 2000 - Sorting values of pivot

Posted on 2011-02-17
4
512 Views
Last Modified: 2013-04-16
Dear Experts,

In the attached file or in Code section there is a macro, which creates pivot and works fine. (with the help of several EE experts :-))

I would search a solution to this part

'Sorting
Range("A11:E22").Select
Selection.Sort Key1:="R11C5", Order1:=xlDescending, Type:=xlSortValues, _
        OrderCustom:=1, Orientation:=xlTopToBottom


Basically it works but just for actual range, and that can change later. Could be this done somehow with referring to pivot attribute? So like maybe the conditional formatting part works, referring to 'Row Grand Total'.

thanks,
Sub PivotCreation()

'Count actual base sheet rows
Dim LastRowWithValue As Long
LastRowWithValue = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

'Source and Place
     ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R" & LastRowWithValue & "C3").CreatePivotTable TableDestination:=ActiveWorkbook.Worksheets("Sheet2").Range("A9"), _
        TableName:="PivotTable1"
'Always
    ActiveWorkbook.Worksheets("Sheet2").Activate
    ActiveSheet.PivotTables("PivotTable1").SmallGrid = False
'Column, Row fields
    ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:= _
        "Item", ColumnFields:="Area"
'Data fields
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Qty")
        .Orientation = xlDataField
        .NumberFormat = "# ##0"
        .Function = xlSum
    End With
'Afterwards Sum if needed
    'ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    '  "Count of Qty").Function = xlSum
'Certain columns not visible
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Area")
        .PivotItems("West").Visible = False
    End With
'Columns replace and order
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Area").PivotItems( _
        "South").Position = 1
'Conditional formatting
ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Row Grand Total'", xlDataAndLabel
    With Selection
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
            , Formula1:="2000"
        .FormatConditions(1).Interior.ColorIndex = 4
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
            Formula1:="2000"
        .FormatConditions(2).Interior.ColorIndex = 3
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotSelect "'Column Grand Total'", xlDataAndLabel
    Selection.FormatConditions.Delete
'Sorting
Range("A11:E22").Select
Selection.Sort Key1:="R11C5", Order1:=xlDescending, Type:=xlSortValues, _
        OrderCustom:=1, Orientation:=xlTopToBottom

'Paste special as values
'    ActiveSheet.PivotTables("PivotTable1").TableRange2.Copy
'    ActiveSheet.PivotTables("PivotTable1").TableRange2.PasteSpecial Paste:=xlPasteValues
End Sub

Open in new window

PivotVBATemplate.xls
0
Comment
Question by:csehz
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 34915383
Try this:
Sub PivotCreation()

'Count actual base sheet rows
Dim LastRowWithValue As Long
Dim PT As PivotTable
LastRowWithValue = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count

'Source and Place
     Set PT = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R" & LastRowWithValue & "C3").CreatePivotTable(TableDestination:=ActiveWorkbook.Worksheets("Sheet2").Range("A9"), _
        TableName:="PivotTable1")
'Always
    With PT
      .Parent.Activate
      .SmallGrid = False
'Column, Row fields
    .AddFields RowFields:="Item", ColumnFields:="Area"
'Data fields
    With .PivotFields("Qty")
        .Orientation = xlDataField
        .NumberFormat = "# ##0"
        .Function = xlSum
    End With
'Afterwards Sum if needed
    'ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    '  "Count of Qty").Function = xlSum
'Certain columns not visible
    .PivotFields("Area").PivotItems("West").Visible = False
'Columns replace and order
    .PivotFields("Area").PivotItems("South").Position = 1
'Conditional formatting
   .PivotSelect "'Row Grand Total'", xlDataAndLabel
    With Selection
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreaterEqual _
            , Formula1:="2000"
        .FormatConditions(1).Interior.ColorIndex = 4
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
            Formula1:="2000"
        .FormatConditions(2).Interior.ColorIndex = 3
    End With
    .PivotSelect "'Column Grand Total'", xlDataAndLabel
    Selection.FormatConditions.Delete
'Sorting
   .PivotFields("Item").AutoSort xlDescending, "Sum of Qty"
End With
'Paste special as values
'    ActiveSheet.PivotTables("PivotTable1").TableRange2.Copy
'    ActiveSheet.PivotTables("PivotTable1").TableRange2.PasteSpecial Paste:=xlPasteValues
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:csehz
ID: 34915418
That works fine :-) Thanks very much
0
 
LVL 1

Author Comment

by:csehz
ID: 34915464
I can just realize now, that you reworked also my beginner logic with defining variable PT as PivotTable, and after all of the pivot attributes based on that.

Super like this, this is the case that I thanked you something, and later understood that should thank more :-)))
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34915475
It's force of habit - I didn't even think about it really! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro 6 49
splitting text of cell to columns 14 24
Access Excel export not behaving 2 25
Manually enter date in datepicker 24 32
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now