How to Sort Multiple Rows in Excel Horizontally

spiros · 1 · 1051

spiros

  • Administrator
  • Hero Member
  • *****
    • Posts: 856486
    • Gender:Male
  • point d’amour
How to Sort Multiple Rows in Excel (2 Ways) - ExcelDemy

Code: [Select]
Sub Sort_Rows()
    Dim xRnge As Range
    Dim yRnge As Range
    If TypeName(Selection) <> "Range" Then Exit Sub
    Set xRnge = Selection

    If xRnge.Count = 1 Then
        MsgBox "Please select multiple cells!", vbExclamation, "Powered by Exceldemy.com"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

    Application.ScreenUpdating = False
    For Each yRnge In xRnge.Rows
        yRnge.Sort Key1:=yRnge.Cells(1, 1), _
        Order1:=xlAscending, _
        Header:=xlNo, _
        Orientation:=xlSortRows
    Next yRnge

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With

    Application.ScreenUpdating = True
End Sub
« Last Edit: 11 Oct, 2022, 11:27:11 by spiros »


 

Search Tools