Macro to sort

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
 
R

ryguy7272

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--
 
J

Joel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top