Looping and advancing

mattmickle

Board Regular
Joined
Nov 17, 2010
Messages
81
Hi all,
I know there has to be an easier more efficient way to do this...
In column B I have random numbers, sorted into "books" of 10. I'm looking to sort each of the "books" ascending, but each book by itself.

I recorded some code, but REALLY don't want to have to hardcode and edit each range of cells for the sort (there are 100 books). Each range advances 10, so sort cells B5:B14, then sort cells B15:B24, then B25:B34, etc. etc. etc.

Any help would be appreciated!



Code:
Sub SORT_BOOKS()'
' SORT_BOOKS Macro
'


'
    'BOOK 1
    Range("B5:B14").Select
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Add Key:=Range( _
        "B5:B14"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Randomizer").Sort
        .SetRange Range("B5:B14")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    'BOOK 2
    Range("B15:B24").Select
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Add Key:=Range( _
        "B15:B24"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Randomizer").Sort
        .SetRange Range("B15:B24")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
Code:
Sub SORT_BOOKS() '
' SORT_BOOKS Macro
   Dim i As Long
   Dim Rng As Range
    
   For i = 5 To Range("B" & Rows.count).End(xlUp).Row Step 10
    Set Rng = Range("B" & i).Resize(10)
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Randomizer").Sort.SortFields.Add Key:=Rng, _
      SortOn:=xlSortOnValues, order:=xlAscending, DataOption:= _
      xlSortNormal
    With ActiveWorkbook.Worksheets("Randomizer").Sort
        .SetRange Rng
        .header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   Next i
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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