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
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

AlphaFrog

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

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,597
Messages
5,512,313
Members
408,886
Latest member
kashifziatevta423

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top