Link to home
Start Free TrialLog in
Avatar of Medicine1
Medicine1Flag for United States of America

asked on

Filtering

This code is working great but I need to add to it   - I need three columns sorted

The First sort would be the code below

The Second sort would be by column D2 ascending which has numbers and text.  I need the number values sorted first in this column the text will say "None" or could be null.

The Third sort would be by Column O2  ascending which is text.


Private Sub sortmainlist()
Dim rng As Range
Dim listnum As Long
 
On Error Resume Next
Application.AddCustomList ListArray:=Array("Top", "Middle", "Bottom")
listnum = Application.GetCustomListNum(Array("Top", "Middle", "Bottom"))

Thank you in advance.
 
With Sheets("Sheet1")
    .Activate
    Set rng = Range(.Cells(2, "A"), .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 5))
End With
 
'Note: listnum+1
rng.Sort Key1:=Range("F2"), Order1:=xlAscending, Header:=xlNo, OrderCustom:=listnum + 1, _
    MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
    DataOption2:=xlSortNormal
     

Application.DeleteCustomList listnum
     
End Sub
Avatar of bromy2004
bromy2004
Flag of Australia image

Could you provide data to test?
Avatar of Medicine1

ASKER

I have included data on sheet2 and code in attached.  Let me know if you need anything else.  

1st sort will be column F
2nd by column B
3rd by column C

Thank you!!
Book3.xlsx
Did you mean something like the attached?
Sub Macro1()
  With ActiveWorkbook.Worksheets("Sheet2").Sort
    .SortFields.Clear
    .SetRange Range("A1:F13")
    .Header = xlYes
    .SortFields.Add Key:=Range("F2:F5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("B2:B5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("C2:C5000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End Sub

Open in new window

Book3.xls
Medicine1,

In this question you started out by saying that you wanted it sorted by:

1st column F
2nd column D
3rd column O

now your most recent requirement is:

1st sort will be column F
2nd by column B
3rd by column C

I am going to take it that in the original file you are now wanting it as your most recent requirement as there is no data in column O.

Patrick
ASKER CERTIFIED SOLUTION
Avatar of patrickab
patrickab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am reattaching the file for the code above.  The Names are not sorting.  I added some names to see.

I told you wrong
The columnas are

F
B
D
sort order
but replacd C with D and did not work...

reorg-09.xls
Never mind  Patrick... looks like yours is working. I have to figure why not working in my sheet....

Thank you....
Works Great
Medicine1 - Thanks for the grade - Patrick