filmov
tv
VBA Sort range with two criteria
![preview_player](https://i.ytimg.com/vi/OnZIxG4zhv0/maxresdefault.jpg)
Показать описание
Sort a range of cells by two criteria or columns.
Code:
Sub sort_range_with_two_criterias()
Dim ws_sheet As Worksheet
Dim lastcolumn As Long
Dim lastrow As Long
'identify worksheet
Set ws_sheet = Worksheets(3)
'find table's limits
lastrow = ws_sheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ws_sheet.Cells(1, Columns.Count).End(xlToLeft).Column
'sort by column A then B
ws_sheet.Range(ws_sheet.Cells(1, 1), ws_sheet.Cells(lastrow, lastcolumn)).Sort _
key1:=ws_sheet.Range("A1"), order1:=xlAscending, _
key2:=ws_sheet.Range("B1"), order2:=xlAscending, Header:=xlYes
End Sub
-------------------------------
NEED HELP?
-------------------------------
Leave a comment with your questions, issues or projetcs and I will try to answer with a video.
-------------------------------
THANK YOU
-------------------------------
For watching my videos,
Your shares
And your comments!
Code:
Sub sort_range_with_two_criterias()
Dim ws_sheet As Worksheet
Dim lastcolumn As Long
Dim lastrow As Long
'identify worksheet
Set ws_sheet = Worksheets(3)
'find table's limits
lastrow = ws_sheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ws_sheet.Cells(1, Columns.Count).End(xlToLeft).Column
'sort by column A then B
ws_sheet.Range(ws_sheet.Cells(1, 1), ws_sheet.Cells(lastrow, lastcolumn)).Sort _
key1:=ws_sheet.Range("A1"), order1:=xlAscending, _
key2:=ws_sheet.Range("B1"), order2:=xlAscending, Header:=xlYes
End Sub
-------------------------------
NEED HELP?
-------------------------------
Leave a comment with your questions, issues or projetcs and I will try to answer with a video.
-------------------------------
THANK YOU
-------------------------------
For watching my videos,
Your shares
And your comments!