Sort left to right rows

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
I had thousand of data which need to Sort left to right rows in numerical order but except s/n

here is the sample data base. (but the row contain data can be as many as this)

is there any fast way to do it?? :eeek: mil thanks.

[TABLE="width: 500"]
<tbody>[TR]
[TD]s/n[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1111[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1112[/TD]
[TD][/TD]
[TD]23[/TD]
[TD][/TD]
[TD]434[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1113[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1114[/TD]
[TD]34[/TD]
[TD][/TD]
[TD]555[/TD]
[TD]555[/TD]
[TD]545[/TD]
[/TR]
[TR]
[TD]1115[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1116[/TD]
[TD]642[/TD]
[TD]3[/TD]
[TD]9[/TD]
[TD][/TD]
[TD]111[/TD]
[/TR]
</tbody>[/TABLE]


I got this code from
https://www.mrexcel.com/forum/excel-questions/693675-rearrange-columns-w-visual-basic-applications-using-row-sort-order.html , it look well but how to excluded the first col?
Code:
Sub SortLtoR()    
    Dim ws As Worksheet
    Dim rng As Range
    
    Set ws = ActiveSheet
    Set rng = ws.UsedRange
    
    With ws.Sort
        With .SortFields
            .Clear
            .Add rng.Rows(1)
        End With
        
        .SetRange rng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        
        .Apply
    End With
End Sub[COLOR=#3E3E3E]
[/COLOR]

 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Re: [NEED HELP] Sort left to right rows

Greetings Harky,

I may be misreading, but if you are simply asking how to change the sort from vertical to horizontal, select the range you want sorted, select Sort and Filter|Custom Sort, and in the dialog, click on Options and change to Left to Right.

Hope that helps,

Mark
 
Upvote 0
Ya
But I can't do it 1by1 as I have thousands row to be sort.

The above script works but I will like to exclude
A column and row for header and s/n

Greetings Harky,

I may be misreading, but if you are simply asking how to change the sort from vertical to horizontal, select the range you want sorted, select Sort and Filter|Custom Sort, and in the dialog, click on Options and change to Left to Right.

Hope that helps,

Mark
 
Upvote 0
I realize you just stated that you cannot do it 1 by 1, but are you actually looking to sort each row individually, albeit by macro? Would the expected results of you sample data in Post #1 be like the below?
Excel Workbook
ABCDE
1s/n
211111
3111223434
41113
5111434545555555
61115
7111639111642
Sheet2
Excel 2010
 
Upvote 0
Yes, to be done using macro.
.. But excluded the row 1 and col A for header and serial.

Hehe... Thanks.

I realize you just stated that you cannot do it 1 by 1, but are you actually looking to sort each row individually, albeit by macro? Would the expected results of you sample data in Post #1 be like the below?
Excel Workbook
ABCDE
1s/n
211111
3111223434
41113
5111434545555555
61115
7111639111642
Sheet2
Excel 2010
 
Last edited:
Upvote 0
Presuming "S/N" is in cell A1:

Before:
Excel Workbook
ABCDEFGH
1S/M
2111188466276883627237661007327
311123086440817428991
41113623187833663
511144385
611156385434072645391366497058592
71116
8111734384048346204669341023
911183277759563144464154699171794
101119312487226554337496031234
Sheet4 (2)
Excel 2010

After:
Excel Workbook
ABCDEFGH
1S/M
2111162237636276100732776888846
311121742308644088991
41113366362318783
511144385
611153664434053916385726485929705
71116
8111740410232046343869348346
911181546179432774464631475959917
101119123431243374655487229603
Sheet4 (2)
Excel 2010

In a junk copy of your workbook, try...

In a Standard Module:
Rich (BB code):
Option Explicit
  
Sub example()
Dim WKS       As Worksheet
Dim rng2Sort  As Range
Dim Index     As Long
  
  Set WKS = ActiveSheet ' or by sheet's tab name ThisWorkbook.Worksheets("Sheet4") or just use Codename
  Set rng2Sort = WKS.Range("A1").CurrentRegion
  Set rng2Sort = rng2Sort.Offset(1, 1).Resize(rng2Sort.Rows.Count - 1, rng2Sort.Columns.Count - 1)
  
  For Index = rng2Sort.Rows.Count To 1 Step -1
    With rng2Sort.Rows(Index)
      .Sort Key1:=.Cells(1), Order1:=xlAscending, Orientation:=xlSortRows
    End With
  Next
  
End Sub

Hope that helps,

Mark
 
Upvote 0
Ergh... I remembered you said that there could be thousands of rows. I'd tack in killing repaints and any calculations to speed it up...

Rich (BB code):
Option Explicit
  
Sub example()
Dim WKS       As Worksheet
Dim rng2Sort  As Range
Dim Index     As Long
Dim CalcMode  As XlCalculation
  
  CalcMode = Application.Calculation
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  
  Set WKS = ActiveSheet ' or by sheet's tab name ThisWorkbook.Worksheets("Sheet4") or just use Codename
  Set rng2Sort = WKS.Range("A1").CurrentRegion
  Set rng2Sort = rng2Sort.Offset(1, 1).Resize(rng2Sort.Rows.Count - 1, rng2Sort.Columns.Count - 1)
  
  For Index = rng2Sort.Rows.Count To 1 Step -1
    With rng2Sort.Rows(Index)
      .Sort Key1:=.Cells(1), Order1:=xlAscending, Orientation:=xlSortRows
    End With
  Next
  
  Application.Calculation = CalcMode
  Application.ScreenUpdating = True
  
End Sub
 
Upvote 0
OMG, it work pretty well!!!
U save me. :beerchug:
it could have spent hour just to sort that :)

Ergh... I remembered you said that there could be thousands of rows. I'd tack in killing repaints and any calculations to speed it up...

Rich (BB code):
Option Explicit
  
Sub example()
Dim WKS       As Worksheet
Dim rng2Sort  As Range
Dim Index     As Long
Dim CalcMode  As XlCalculation
  
  CalcMode = Application.Calculation
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = False
  
  Set WKS = ActiveSheet ' or by sheet's tab name ThisWorkbook.Worksheets("Sheet4") or just use Codename
  Set rng2Sort = WKS.Range("A1").CurrentRegion
  Set rng2Sort = rng2Sort.Offset(1, 1).Resize(rng2Sort.Rows.Count - 1, rng2Sort.Columns.Count - 1)
  
  For Index = rng2Sort.Rows.Count To 1 Step -1
    With rng2Sort.Rows(Index)
      .Sort Key1:=.Cells(1), Order1:=xlAscending, Orientation:=xlSortRows
    End With
  Next
  
  Application.Calculation = CalcMode
  Application.ScreenUpdating = True
  
End Sub
 
Upvote 0
Whilst I cannot swear to above 2010, it should.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top