vba sort array row by row

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I have an array B2:F9500 dynamic.

short example
this is what I have
1626140579610.png


this is what I am looking for

1626140615772.png

in other words, I need to sort in ascending order each row.
thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:

VBA Code:
Sub SortNums()
  Dim a As Variant, b As Variant
  Dim coll As Object
  Dim i As Long, j As Long
  
  a = Range("B2", Cells(Range("F" & Rows.Count).End(3).Row, Cells(2, Columns.Count).End(1).Column)).Value
  
  Set coll = CreateObject("System.Collections.ArrayList")
  For i = 1 To UBound(a)
    For j = 1 To UBound(a, 2)
      coll.Add a(i, j)
    Next
    coll.Sort
    For j = 0 To coll.Count - 1
      a(i, j + 1) = coll(j)
    Next
    coll.Clear
  Next
  Range("B2").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub SortLtoR()

Dim i As Long, lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lr
    Range("A" & i & ":E" & i).Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("A" & i & ":E" & i) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A" & i & ":E" & i)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
Next i
End Sub
 
Upvote 0
Like this?

VBA Code:
Sub Sort_By_Row()
  Dim rw As Range
  
  Application.ScreenUpdating = False
  For Each rw In Range("B2:F" & Range("B" & Rows.Count).End(xlUp).Row).Rows
    rw.Sort Key1:=rw, Order1:=xlAscending, Orientation:=xlLeftToRight
  Next rw
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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