Macro Sort on two Fields

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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