PDA

View Full Version : range sort horizontal range



randino
01-05-2021, 01:52 PM
Trying to sort a 1 row multiple column range it does seem to work.
So far I have this

Dim vHeader As Variant
Dim rFirstHeading As Range
Set rFirstHeading = Range("B4")
Dim i As Integer
Dim iRow As Integer
iRow = rFirstHeading.Row
Dim iColumn As Integer
iColumn = rFirstHeading.Column

For i = 1 To rColHeaders.Count
Cells(iRow, iColumn).Value = rColHeaders(i)
iColumn = iColumn + 1
Next
' Arange headins in assending order
Dim rHeadings As Range
Set rHeadings = rFirstHeading.Resize(1, rColHeaders.Count)
rHeadings.Sort Key1:=Range("B4"), order1:=xlAscending, Orientation:=xlSortColumns
rHeadings.Sort Orientation:=xlSortColumns



essentially it is Range("B4:F4").sort which give Sort method of range class failed
in the code, the first one runs, but I don't see any results.

jolivanes
01-05-2021, 11:47 PM
Re: "essentially it is Range("B4:F4")"
Try this.

Range(Cells(4, 2), Cells(4, 6)).Sort Key1:=Range(Cells(4, 2), Cells(4, 6)), Order1:=xlDescending, Orientation:=xlLeftToRight '<----- or Ascending

snb
01-06-2021, 01:48 AM
@joli


Range(Cells(4, 2), Cells(4, 6)).Sort
=

cells(4, 2).resize(,4).Sort

randino
01-06-2021, 06:27 AM
That works. Thanks!
Why did you use choose Cells? Should I be using cells rather than range?

randino
01-06-2021, 06:33 AM
Neither one worked for me. Run-time error 1004 Sort method of range class failed.
Thanks for the link, lots of good information.

jolivanes
01-06-2021, 03:04 PM
@snb
Of course! Thanks (Copied it from an old file without paying too much attention. Should know better though.)
Use Cells or Range. Don't worry about the difference. Whatever your preference is.
My personal preference is Cells