Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to sort right to left Steve Excel Discussion (Misc queries) 1 April 3rd 09 01:40 AM
SORT BY: left to right Shevvie Excel Worksheet Functions 0 June 24th 08 02:47 PM
Macro to sort numeric rows left to right Carlton A. Barlow Excel Discussion (Misc queries) 14 December 3rd 07 04:41 AM
Sort Left-Right & Up-Down Ben Dummar Excel Worksheet Functions 0 April 4th 07 05:56 PM
Sort Left to Right David Excel Discussion (Misc queries) 1 February 3rd 05 03:18 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"