Sorting - Finding last row

W

WLMPilot

I want a commandbutton to sort A3:Q. Though column Q is included in the
range, the cells in column Q may be empty. Therefore, I need to locate the
last row using column A, then sort A3:Q(lastrow).

Here is the code I was working with:

Private Sub CommandButton3_Click()
Lastrow = Range("A65536").End(xlUp).Select
Range("A3:Q" & Lastrow).Select
Selection.Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom
End Sub

Thanks for your help!

Les
 
S

Sheeloo

Use
LastRow = Range("A65536").End(xlUp).Row
instead of
Lastrow = Range("A65536").End(xlUp).Select
 
B

Barb Reinhardt

If you have any intention of moving to Excel 2007, I'd recommend doing this

Dim aWS as Worksheet

set aWS = activesheet

lastRow = aws.cells(aws.rows.count,1).end(xlup).row

--
HTH,
Barb Reinhardt

If this post was helpful to you, please click YES below.
 

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