ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to Sort Left to Right (https://www.excelbanter.com/excel-programming/386399-macro-sort-left-right.html)

Bugaboo

Macro to Sort Left to Right
 
I am trying to write a macro that will sort each row separately left to
right. The macro recorder works fine if the number of columns stays the
same. Because the numer of columns will vary, I need to write it so it
selects everything from column B to the last column.

This is what I have

Category 1 458 681 695 744
Category 2 23619 24783 24863 22109
Category 3 -916 1283 358 329

It should look like this after it is sorted

Category 1 681 744 458 695
Category 2 22109 23619 24783 24863
Category 3 -916 329 358 1283

Thanks.





Vergel Adriano

Macro to Sort Left to Right
 
Assuming data starts at row 1 and that while the number of columns will vary,
all rows will always have the same number of columns. Something like this
would work:

Sub test()
Dim lLastRow As Long
Dim iLastColumn As Integer
Dim lCurrentRow As Long

With ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastC ell)
lLastRow = .Row
iLastColumn = .Column
End With

For lCurrentRow = 1 To lLastRow
With ActiveSheet
.Range(Cells(lCurrentRow, 2), Cells(lCurrentRow,
iLastColumn)).Sort key1:=Range("B" & lCurrentRow), Orientation:=xlLeftToRight
End With
Next lCurrentRow

End Sub



--
Hope that helps.

Vergel Adriano


"Bugaboo" wrote:

I am trying to write a macro that will sort each row separately left to
right. The macro recorder works fine if the number of columns stays the
same. Because the numer of columns will vary, I need to write it so it
selects everything from column B to the last column.

This is what I have

Category 1 458 681 695 744
Category 2 23619 24783 24863 22109
Category 3 -916 1283 358 329

It should look like this after it is sorted

Category 1 681 744 458 695
Category 2 22109 23619 24783 24863
Category 3 -916 329 358 1283

Thanks.





Bugaboo

Macro to Sort Left to Right
 
Thanks. It worked great!



"Vergel Adriano" wrote:

Assuming data starts at row 1 and that while the number of columns will vary,
all rows will always have the same number of columns. Something like this
would work:

Sub test()
Dim lLastRow As Long
Dim iLastColumn As Integer
Dim lCurrentRow As Long

With ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastC ell)
lLastRow = .Row
iLastColumn = .Column
End With

For lCurrentRow = 1 To lLastRow
With ActiveSheet
.Range(Cells(lCurrentRow, 2), Cells(lCurrentRow,
iLastColumn)).Sort key1:=Range("B" & lCurrentRow), Orientation:=xlLeftToRight
End With
Next lCurrentRow

End Sub



--
Hope that helps.

Vergel Adriano


"Bugaboo" wrote:

I am trying to write a macro that will sort each row separately left to
right. The macro recorder works fine if the number of columns stays the
same. Because the numer of columns will vary, I need to write it so it
selects everything from column B to the last column.

This is what I have

Category 1 458 681 695 744
Category 2 23619 24783 24863 22109
Category 3 -916 1283 358 329

It should look like this after it is sorted

Category 1 681 744 458 695
Category 2 22109 23619 24783 24863
Category 3 -916 329 358 1283

Thanks.






All times are GMT +1. The time now is 08:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com