Sort selected range

  • Thread starter Thread starter J.W. Aldridge
  • Start date Start date
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
 
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
 
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"
 
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).
 
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
 
Back
Top