Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
macro to sort right to left | Excel Discussion (Misc queries) | |||
SORT BY: left to right | Excel Worksheet Functions | |||
Macro to sort numeric rows left to right | Excel Discussion (Misc queries) | |||
Sort Left-Right & Up-Down | Excel Worksheet Functions | |||
Sort Left to Right | Excel Discussion (Misc queries) |