Sort selected range

J

J.W. Aldridge

How do I input the code to sort the selected range from here?

The range may be any row, so leave that portion open/variable.


Range("D65534").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlToRight)).Select
 
B

Bob Phillips

Set rng = Range("D65534").End(xlUp)
Set rng = Range(rng, rng.End(xlToRight))
rng.Sort Key1:=rng.Cells(1, 1), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
DataOption1:=xlSortNormal
 
J

J.W. Aldridge

getting error on this portion....

rng.Sort Key1:=rng.Cells(1, 1), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight,
_
DataOption1:=xlSortNormal


"application defined or object defined error"
 
R

Rick Rothstein

That range Set will "break" if there are any blank cells in Column D's last
filled row anywhere to the right of Column D. This Set statement should work
for that condition as well as a range with no blank cells in the indicated
row)...

Set rng = Cells(Rows.Count, "D").End(xlUp).Resize(, Columns.Count - 3)

The 3 at the end is one less than the column's number value (Column "D" in
this case, which is column number 4, and one less than that is the 3 that I
used).
 
B

Bob Phillips

See if this is better

Set rng = Range("D65534").End(xlUp)
Set rng = Range(rng, rng.End(xlToRight))
rng.Sort Key1:=rng.Cells(1, 1), _
Order1:=xlAscending, _
Header:=xlGuess, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlLeftToRight, _
DataOption1:=xlSortNormal
 

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