• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Sorting in Excel VBA

coolkiran

Member
I wanted to know, is that possible in excel,

I have 2 sheets, one is database that will update daily, in that sheet Column A is Driver name.

In second sheet i have unique Driver name and in second column i have entered orders of driver i needed. Say example Driver A should be in 2 order, Driver B should be at 1, I mean sorting,

I have attached sample file.

Let me know is that possible.
 

Attachments

  • Chandoo.xlsx
    9.5 KB · Views: 1
How's this? Creates a helper column, sorts on that column, then clears helper column.
Code:
Sub CustomSort()
Dim lastRow As Long

Application.ScreenUpdating = False
With Worksheets("Data")
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   
    .Range("C1").Value = "Helper"
    .Range("C2:C" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[-2],'Sort Rules'!C[-2]:C[-1],2,)"
   
    With .Sort
        .SortFields.Clear
        .SortFields.Add Key:=Worksheets("Data").Range("C1"), SortOn:=xlSortOnValues, _
            Order:=xlAscending, DataOption:=xlSortTextAsNumbers
   
        .SetRange Range("A2:C13")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    .Columns("C:C").ClearContents
End With
Application.ScreenUpdating = True
End Sub
 
This is a simple solution depending on creating custom list
Code:
Sub SortCustomList()
    Application.AddCustomList Array("G", "D", "B", "A", "F", "C")
    Sheets("Data").Range("A1").CurrentRegion.Sort key1:=Range("A1"), OrderCustom:=Application.CustomListCount + 1, Header:=xlYes
    Application.DeleteCustomList Application.CustomListCount
End Sub
 
Back
Top