Sort multiple rows horizontally and independently.

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
I have a table of numbers with many rows. (1918 rows)
Column A has a date, numbers are from columns B:F

If cell A2 has a date, I would like to sort B2:F2 independently.
Then look down into the next row.
If cell A3 has a date, I would like to sort B3:F3 independently.
And so on until the macro doesn't see a date in column A.

I recorded a Macro which will only do one row at a time.

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
    Range("B2:F2").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B2:F2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B2:F2")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this on a copy of your Workbook as unexpected results may occur
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
Dim cell As Range

For Each cell In Range("A1:A" & Worksheets("Sheet1").Rows.Count)
If IsDate(cell.Value) Then
    Range("B" & cell.Row & ":F" & cell.Row).Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B" & cell.Row & ":F" & cell.Row) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B" & cell.Row & ":F" & cell.Row)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
End If
Next cell
End Sub
 
Upvote 0
Edit
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'
Dim cell As Range
Application.ScreenUpdating = False
For Each cell In Range("A1:A" & Worksheets("Sheet1").Rows.Count)
If IsDate(cell.Value) Then
    Range("B" & cell.Row & ":F" & cell.Row).Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B" & cell.Row & ":F" & cell.Row) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B" & cell.Row & ":F" & cell.Row)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
End If
Next cell
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Similar approach to @Skyybot , tested at 2K rows at around 0.5 seconds.
VBA Code:
Option Explicit
Sub L_to_R()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, c As Range
    Set ws = Worksheets("Sheet1")
    For Each c In ws.Range("A2", Cells(Rows.Count, "A").End(xlUp))
        If IsDate(c) Then c.Offset(, 1).Resize(, 5).Sort _
        Key1:=c, Order1:=xlAscending, Orientation:=xlLeftToRight
        ws.Sort.SortFields.Clear
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

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