Sorts a range or area by 1 column given its cell address, with options to choose, order, sheet, workbook
Sub SortArea1(Sorted_Range, SortByCell, Optional Order_Asc1_Desc2 = 1, Optional SheetName = "This", optional WBName = "This")
' Sorted_Range is the actual area to be sorted, like A4:H200
' SoryByCell is the column to sort by, like B4
If WBName = "This" then WBName = Thisworkbook.Name
If SheetName = "This" then SheetName = Activesheet.name
OOrd = xlAscending
If Order_Asc1_Desc2 = 2 Then OOrd = xlDescending
Workbooks(WBName).Worksheets(SheetName).Sort.SortFields.Clear
Workbooks(WBName).Worksheets(SheetName).Sort.SortFields.Add Key:=Workbooks(WBName).Worksheets(SheetName).Range(SortByCell), SortOn:=xlSortOnValues, Order:=OOrd, DataOption:=xlSortNormal
With Workbooks(WBName).Worksheets(SheetName).Sort
.SetRange Workbooks(WBName).Worksheets(SheetName).Range(Sorted_Range)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
' Sorted_Range is the actual area to be sorted, like A4:H200
' SoryByCell is the column to sort by, like B4
If WBName = "This" then WBName = Thisworkbook.Name
If SheetName = "This" then SheetName = Activesheet.name
OOrd = xlAscending
If Order_Asc1_Desc2 = 2 Then OOrd = xlDescending
Workbooks(WBName).Worksheets(SheetName).Sort.SortFields.Clear
Workbooks(WBName).Worksheets(SheetName).Sort.SortFields.Add Key:=Workbooks(WBName).Worksheets(SheetName).Range(SortByCell), SortOn:=xlSortOnValues, Order:=OOrd, DataOption:=xlSortNormal
With Workbooks(WBName).Worksheets(SheetName).Sort
.SetRange Workbooks(WBName).Worksheets(SheetName).Range(Sorted_Range)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sorted_Range, SortByCell, Optional Order_Asc1_Desc2, Optional SheetName, optional WBName
Sorted_Range is the actual area to be sorted, like A4:H200
SoryByCell is the column to sort by, like B4
SoryByCell is the column to sort by, like B4
Views 3,271
Downloads 1,297
CodeID
DB ID
ANmarAmdeen
613
Revisions
v1.0
Wednesday
July
4
2018