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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi, I back again.

This code work well but can help me to improve more :D

Previous, it was use to sort in numeric order. Can add to sort base on alphabetical order too?

Notice it cant sort base on alphabetical order, e.g ABC123


Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
  
[COLOR=darkblue]Sub[/COLOR] example()
[COLOR=darkblue]Dim[/COLOR] WKS       [COLOR=darkblue]As[/COLOR] Worksheet
[COLOR=darkblue]Dim[/COLOR] rng2Sort  [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]Dim[/COLOR] Index     [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
[COLOR=darkblue]Dim[/COLOR] CalcMode  [COLOR=darkblue]As[/COLOR] XlCalculation
  
  CalcMode = Application.Calculation
  Application.Calculation = xlCalculationManual
  Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
  
  [COLOR=darkblue]Set[/COLOR] WKS = ActiveSheet [COLOR=green]' or by sheet's tab name ThisWorkbook.Worksheets("Sheet4") or just use Codename[/COLOR]
  [COLOR=darkblue]Set[/COLOR] rng2Sort = WKS.Range("A1").CurrentRegion
  [COLOR=darkblue]Set[/COLOR] rng2Sort = rng2Sort.Offset(1, 1).Resize(rng2Sort.Rows.Count - 1, rng2Sort.Columns.Count - 1)
  
  [COLOR=darkblue]For[/COLOR] Index = rng2Sort.Rows.Count [COLOR=darkblue]To[/COLOR] 1 [COLOR=darkblue]Step[/COLOR] -1
    [COLOR=darkblue]With[/COLOR] rng2Sort.Rows(Index)
      .Sort Key1:=.Cells(1), Order1:=xlAscending, Orientation:=xlSortRows
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
  [COLOR=darkblue]Next[/COLOR]
  
  Application.Calculation = CalcMode
  Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
  
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
You are very welcome :-)


Hi, Thank for the marco but i need some help to improve it on the sorting part.
Can i sort as show below? But do take note on the one in red.


"S/N" is in cell A1:

Before:
Sheet4 (2)[TABLE="class: html-maker-worksheet"]
<thead>[TR]
[TH="align: center"][/TH]
[TH="align: center"]A[/TH]
[TH="align: center"]B[/TH]
[TH="align: center"]C[/TH]
[TH="align: center"]D[/TH]
[TH="align: center"]E[/TH]
[TH="align: center"]F[/TH]
[TH="align: center"]G[/TH]
[TH="align: center"]H[/TH]
[/TR]
</thead><tbody>[TR]
[TH="align: center"]1[/TH]
[TD]S/M[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]2[/TH]
[TD="align: right"]1111[/TD]
[TD="align: right"]8846[/TD]
[TD="align: right"]62[/TD]
[TD="align: right"]7688e[/TD]
[TD="align: right"]7688[/TD]
[TD="align: right"]2376[/TD]
[TD="align: right"]6100[/TD]
[TD="align: right"]6100a[/TD]
[/TR]
[TR]
[TH="align: center"]3[/TH]
[TD="align: right"]1112[/TD]
[TD="align: right"]3086[/TD]
[TD][/TD]
[TD="align: right"]3086a[/TD]
[TD][/TD]
[TD="align: right"]ABC[/TD]
[TD="align: right"]8991[/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]4[/TH]
[TD="align: right"]1113[/TD]
[TD="align: right"]6231[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]8783[/TD]
[TD="align: right"]3663[/TD]
[/TR]
[TR]
[TH="align: center"]5[/TH]
[TD="align: right"]1114[/TD]
[TD="align: right"]4385[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]6[/TH]
[TD="align: right"]1115[/TD]
[TD="align: right"]6385[/TD]
[TD="align: right"]4340[/TD]
[TD="align: right"]7264[/TD]
[TD="align: right"]5391[/TD]
[TD="align: right"]3664[/TD]
[TD="align: right"]9705[/TD]
[TD="align: right"]8592[/TD]
[/TR]
[TR]
[TH="align: center"]7[/TH]
[TD="align: right"]1116[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]8[/TH]
[TD="align: right"]1117[/TD]
[TD="align: right"]3438[/TD]
[TD][/TD]
[TD="align: right"]404[/TD]
[TD="align: right"]8346[/TD]
[TD="align: right"]2046[/TD]
[TD="align: right"]6934[/TD]
[TD="align: right"]1023[/TD]
[/TR]
[TR]
[TH="align: center"]9[/TH]
[TD="align: right"]1118[/TD]
[TD="align: right"]3277[/TD]
[TD="align: right"]7595[/TD]
[TD="align: right"]6314[/TD]
[TD="align: right"]4464[/TD]
[TD="align: right"]1546[/TD]
[TD="align: right"]9917[/TD]
[TD="align: right"]1794[/TD]
[/TR]
[TR]
[TH="align: center"]10[/TH]
[TD="align: right"]1119[/TD]
[TD][/TD]
[TD="align: right"]3124[/TD]
[TD="align: right"]8722[/TD]
[TD="align: right"]6554[/TD]
[TD="align: right"]3374[/TD]
[TD="align: right"]9603[/TD]
[TD="align: right"]1234[/TD]
[/TR]
</tbody>[/TABLE]


After:
Sheet4 (2)
[TABLE="class: html-maker-worksheet"]
<thead>[TR]
[TH="align: center"][/TH]
[TH="align: center"]A[/TH]
[TH="align: center"]B[/TH]
[TH="align: center"]C[/TH]
[TH="align: center"]D[/TH]
[TH="align: center"]E[/TH]
[TH="align: center"]F[/TH]
[TH="align: center"]G[/TH]
[TH="align: center"]H[/TH]
[/TR]
</thead><tbody>[TR]
[TH="align: center"]1[/TH]
[TD]S/M[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]2[/TH]
[TD="align: right"]1111[/TD]
[TD="align: right"]62[/TD]
[TD="align: right"]2376[/TD]
[TD="align: right"]6100[/TD]
[TD="align: right"]6100a[/TD]
[TD="align: right"]7688[/TD]
[TD="align: right"]7688e[/TD]
[TD="align: right"]8846[/TD]
[/TR]
[TR]
[TH="align: center"]3[/TH]
[TD="align: right"]1112[/TD]
[TD="align: right"]ABC[/TD]
[TD="align: right"]3086[/TD]
[TD="align: right"]3086a[/TD]
[TD="align: right"]8991[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]4[/TH]
[TD="align: right"]1113[/TD]
[TD="align: right"]3663[/TD]
[TD="align: right"]6231[/TD]
[TD="align: right"]8783[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]5[/TH]
[TD="align: right"]1114[/TD]
[TD="align: right"]4385[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]6[/TH]
[TD="align: right"]1115[/TD]
[TD="align: right"]3664[/TD]
[TD="align: right"]4340[/TD]
[TD="align: right"]5391[/TD]
[TD="align: right"]6385[/TD]
[TD="align: right"]7264[/TD]
[TD="align: right"]8592[/TD]
[TD="align: right"]9705[/TD]
[/TR]
[TR]
[TH="align: center"]7[/TH]
[TD="align: right"]1116[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]8[/TH]
[TD="align: right"]1117[/TD]
[TD="align: right"]404[/TD]
[TD="align: right"]1023[/TD]
[TD="align: right"]2046[/TD]
[TD="align: right"]3438[/TD]
[TD="align: right"]6934[/TD]
[TD="align: right"]8346[/TD]
[TD][/TD]
[/TR]
[TR]
[TH="align: center"]9[/TH]
[TD="align: right"]1118[/TD]
[TD="align: right"]1546[/TD]
[TD="align: right"]1794[/TD]
[TD="align: right"]3277[/TD]
[TD="align: right"]4464[/TD]
[TD="align: right"]6314[/TD]
[TD="align: right"]7595[/TD]
[TD="align: right"]9917[/TD]
[/TR]
[TR]
[TH="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: center"]10[/TH]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]1119[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]1234[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]3124[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]3374[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]6554[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]8722[/TD]
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=FAFAFA]#FAFAFA[/URL] , align: right"]9603[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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