Macro to sort

  • Thread starter Thread starter WLMPilot
  • Start date Start date
W

WLMPilot

I need a macro that will take a list and sort by last name (B:B) with the
first row of data being row 3. The user will be adding names at the end of
the list and then press a command button to sort in ascending order. I know
xlUp needs to be used but not sure how to place it in the code. The column
range is A:N. Column headers are in row 2.

Thanks,
Les
 
Recorded Macro:
Sub Macro1()

Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

Slightly different style (to find the last used cell):
Sub Macro1()

Cells(Rows.Count, "A").End(xlUp).Select
Range(Selection, Selection.End(xlUp)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

Regards,
Ryan--
 
FirstRow = 3
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Set SortRange = Range("A" & FirstRow & ":N" & LastRow)
SortRange.Sort _
Key1:=Range("B1"), _
Order1:=xlAscending, _
Header:=xlGuess
 
Back
Top