Solved

VBA Excel 2000 - Sorting values of pivot

Posted on 2011-02-17
4
534 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

856 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