Macro Sort on two Fields

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have text in Col F (various categories) from row 2 onwards (Headers in in row1) and values in Col G


I would like a macro to sort Col F from row2 onwards in Ascending order and then to sort the values for each of the categories from Largest to smallest

I have written code to do this, but get a run time error 1004 "Sort method of range class failed"

Code:
 Selection.Sort Key2:=Range("F2"), Order1:=xlDescending




Code:
 Sub Sort_Largest()
Sheets(3).Select
Dim Lr As Long
Lr = Cells(Rows.Count, "C").End(xlUp).Row
Range("A2:H" & Lr).Select
Selection.Sort Key1:=Range("G2"), Order1:=xlAscending
Selection.Sort Key2:=Range("F2"), Order1:=xlDescending


End Sub


It would be appreciated if someone could kindly amend my code
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Possibly...

Code:
[CODE]Sub Sort_Largest()
    Sheets(3).Select
    Dim Lr As Long
    Lr = Cells(Rows.Count, "C").End(xlUp).Row
    Range("A2:H" & Lr).Sort Key1:=Range("G2"), Order1:=xlAscending, _
    Key2:=Range("F2"), Order1:=xlDescending
End Sub
[/CODE]
 
Upvote 0
Thanks for the help. Code works but does not goes me the desired result


I have recorded the macro to sort and it gives me the desired result


Kindly neaten the code


Code:
 Sub Sort_Data()
Sheets(3).Select

'
    Columns("A:H").Select
    ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:= _
        Range("G1:G13935"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Invoices").Sort.SortFields.Add Key:= _
        Range("F1:F13935"), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Invoices").Sort
        .SetRange Range("A1:H13935")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
  
End Sub
 
Upvote 0
What do you want "neatened" with the code, there is nothing wrong with it?
 
Upvote 0
Thanks for the reply

I have streamlined it as follows:

Code:
 Sub Sort_Data()
Sheets(3).Select
    Dim Lr As Long
    Lr = Cells(Rows.Count, "C").End(xlUp).Row
   ActiveWorkbook.Worksheets(3).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(3).Sort.SortFields.Add Key:= _
        Range("G1:G" & Lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets(3).Sort.SortFields.Add Key:= _
        Range("F1:F" & Lr), SortOn:=xlSortOnValues, Order:=xlDescending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets(3).Sort
        .SetRange Range("A1:H" & Lr)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top