Vba - Sorting variable range of cells

vrobinson0304

New Member
Joined
Jul 5, 2011
Messages
14
I have a spreadsheet that has multiple rows of data that are grouped between a header and blank cell. I am trying to write a macro to run through column B to search for the headers (there are 5 possible headers) and select the cell under it until the last cell before a blank cell and sort in ascending order. I am new to vba and can usually peice together code, but am having a roadblock here.

header1
a
b
c
blank cell

header2
x
y
z
blank cell

Thank you in advance!
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:
Code:
Sub SortB()

    Dim headers As Variant, header As Variant
    Dim headerCell As Range, blankCell As Range
    
    headers = Array("header1", "header2", "header3", "header4", "header5")
    
    For Each header In headers
        Set headerCell = Columns("B:B").Find(What:=header, After:=Range("B1"), LookIn:=xlFormulas, _
            LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
        If Not headerCell Is Nothing Then
            Set headerCell = headerCell.Offset(1, 0)
            Set blankCell = Columns("B:B").Find(What:="", After:=headerCell, LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
            
            Range(headerCell, blankCell).Sort Key1:=headerCell, Order1:=xlAscending, header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
       
        End If
        
    Next

End Sub
 
Upvote 0
Thank you this works. However, I need it to sort the entire rows. Right now it is only sorting the cells in column B. Please advise.

Thanks again!
 
Upvote 0
The Macro Recorder helps with the syntax for sorting rows. Change the Sort statement to:
Code:
            Rows(headerCell.Row & ":" & blankCell.Row).Sort Key1:=headerCell, Order1:=xlAscending, header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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