VBA to sort rows selected by column value

Mark F

Well-known Member
Joined
Jun 7, 2002
Messages
515
Office Version
  1. 365
Platform
  1. Windows
The code below, kindly provided / supported by others in the group identifies / selects a range of rows where the contents in Column C match

It will then sort those rows using the sort criteria included

What I would like to be able to do is to somehow modify the code so that each subsequent block of rows of the same value in column C are sorted

For example rows 10 to 19 have value 1 in column C, so the code selects this range and sorts
Next , row 20 to 34 have the value 2 in column C so the code will select that range and sort, and so on until the last row of the sheet

Could you please help with addition / modification of the code

Code:
Sub SortSubDeptAlphabetically2()    'ALPHABETICALLY
    On Error Resume Next
    Application.ScreenUpdating = False
    
    Dim firstRow    As Long
    Dim lastRow     As Long
    Dim actCelRow   As Long
    Dim colCnumber  As Long
    
    actCelRow = ActiveCell.Row
    colCnumber = Cells(actCelRow, 3)
    
    firstRow = actCelRow
    If firstRow < 9 Then firstRow = 9
    While Not IsEmpty(Cells(firstRow, 3)) _
      And Cells(firstRow, 3) = colCnumber
        firstRow = firstRow - 1
    Wend
    
    lastRow = actCelRow
    If lastRow <= 9 Then lastRow = 9
    While Not IsEmpty(Cells(lastRow, 3)) _
      And Cells(lastRow, 3) = colCnumber
        lastRow = lastRow + 1
    Wend
       
    Range(Cells(firstRow + 1, 1), Cells(lastRow - 1, "AY")).Select
    
     With Selection
        .Sort Key1:=.Cells(firstRow, 7), Order1:=xlAscending, _
              key2:=.Cells(firstRow, 24), order2:=xlAscending, Header:=xlNo, key3:=.Cells(firstRow, 8), order3:=xlAscending, Header:=xlNo
    End With
    Range("a7:bb7").Select
    ActiveWindow.ScrollRow = firstRow - 7
    
    Range("ab" & firstRow + 1).Select
    
    Application.ScreenUpdating = True
End Sub

Many Thanks
Mark
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I'm not sure whether my explanation / code about really explained things, and what I'm hoping to achieve

Any help much appreciated

Mark
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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