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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,450
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
 

steve the excel guy

Board Regular
Joined
Aug 28, 2007
Messages
104
Thank you very much. That seems to work. I will be experimenting to see if it will sort up to 800 rows. Thanks again!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,746
Messages
5,833,448
Members
430,209
Latest member
addms

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
Top