Sorting many rows (not columns)

steve the excel guy

Board Regular
Joined
Aug 28, 2007
Messages
104
Ok, I want to be able to sort many rows (over 800) alphabetically where Column A is the header column. I recorded a small sample macro to get an idea of what the code looks like. There is no option to leave Column A open as a header so for example only, I highlighted B1:H1, then sorted, then B2:H2, then sorted. Below is what it looks like. I would actually want the entire row (minus A) to be sorted to around 800 rows.

I want to add some kind of loop in here.

Code:
Sub sort1()
'
' sort1 Macro
'

'
    Range("B1:H1").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:H1"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B1:H1")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B2:H2").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:H2"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("B2:H2")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I'm not sure I understand how you want it all sorted, but here's a guess.
Each row sorted individually left to right ascending columns B:H

Code:
Sub Sort1()
'
' Sort1 Macro
'
        
    Dim r As Long, Lastrow As Long
    
    Application.ScreenUpdating = False
    
    With Worksheets("Sheet1")
    
        Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
        
        For r = 1 To Lastrow
            
            .Range("B" & r).Resize(1, 7).Sort Key1:=.Range("B" & r), Order1:=xlAscending, MatchCase:=False, _
                                              Orientation:=xlLeftToRight, DataOption1:=xlSortNormal
            
        Next r

    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,211,789
Messages
6,103,985
Members
447,888
Latest member
eaydogan

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