PaoLacan
PaoLacan

Reputation: 1

Sorting data that starts with

Is there a way to sort data in Excel using VBA where in the data that starts with, for example, 01A should be the top, then all 01B, etc. Please do let me know if I need to provide more info or if I need to explain further. Thank you!

Upvotes: 0

Views: 180

Answers (2)

Ryan Nguyen
Ryan Nguyen

Reputation: 25

Hope this is the one you looking for.

Sub sbSortDataInExcelInDescendingOrder()
Dim strDataRange, strkeyRange As String
strDataRange = "C1:F6"
strkeyRange = "D2:D6"
With Sheets("Sheet1").Sort
.SortFields.Clear
.SortFields.Add _
    Key:=Range(strkeyRange), _
    SortOn:=xlSortOnValues, _
    Order:=xlDescending, _
    DataOption:=xlSortNormal

 .SetRange Range(strDataRange)
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
 End With
 End Sub

Upvotes: 0

Jagadish Dabbiru
Jagadish Dabbiru

Reputation: 940

Range.Sort is a way to sort data in excel using vba.

Syntax:-

expression .Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)

Ex:-

Dim oneRange As Range
Dim aCell As Range
Set oneRange = Range("A1:G12")
Set aCell = Range("A2")
oneRange.Sort Key1:=aCell, Order1:=xlAscending, Header:=xlYes

For elaborate explanation you can click here

Upvotes: 1

Related Questions