Looping and sorting through blocks of rows

zeekmcphee

New Member
Joined
Feb 27, 2018
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello
I am looking for a VBA solution
I have a sheet with several thousand rows.The rows are split up into blocks of rows and seperated by an empty row.Each block of rows will vary with entries so these are random

The sheet has 7 fields author,book_ title,publisher,year_published,genre,number,copies_sold.
The number field is a number against each book title within the block of rows

The last field..copies_sold is a numeric field and what I would like to do is sort each authors copies sold field from least sold to most sold.
If the sheet was one long continuous record then sorting would be easy,but it is a series of blocks of records for each author.
Is there a way that I could some how loop through each individual authors block and sort the copies sold field from smallest to largest;
Below is an example of just three authors,there are three blocks of rows of 4,6and7

author_name.. book_title..publisher..year_publishednumbergenrecopies_sold
molly brownswandolphin20141mystery26000
molly brownthe rockdolphin20132mystery18500
molly browndaytimedolphin20123thriller670500
molly brownlostgracey20064mystery78000
empty
empty
empty empty empty
empty
empty
ken daviessunshinegracey20081crime136000
ken daviesrocketgracey20062crime236000
ken davieslast one ingracey20073mystery900000
ken davieswaterfallbell20104mystery155000
ken daviesthe ringbell20165thriller257893
ken daviesaprilbell20176thriller823456
emptyempty
empty
empty
empty
empty
empty
dawn ellismrs jonesbell 20171bio289000
dawn ellisthe paradegracey20172bio456000
dawn elliscarnivalgracey20143thriller234000
dawn ellisvalleydolphin20124mystery980000
dawn ellismy treedolphin20105romance345000
dawn ellistalkdolphin20096romance650000
dawn ellissmilebell20037thriller345000

<tbody>
</tbody>

Hope this makes sense :)
ZM
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Use this on a copy first.

You will need to remove the headings from row one to start with so the first Author starts in row 2 Just copy to one side..but not connected to column 7

It assumes you are on the activesheet.

Code:
Sub KWSorted()


Dim i As Long
Dim LastRow As Long


LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To LastRow


    If Cells(i, 1) = "" Then i = i + 1
    If Cells(i - 1, 1) <> "" Then
        i = i + 1
    Else
        ActiveSheet.Cells(i, 1).CurrentRegion.Select
        Selection.Sort key1:=Cells(i, 7), order1:=xlAscending
    End If


Next i


End Sub
 
Upvote 0
zeekmcphee,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider, that is based on your flat text display, that will run in the active worksheet.

Please test this first on a copy of your original workbook/worksheet.

Code:
Sub zeekmcphee()
' hiker95, 02/27/2018, ME1045426
Application.ScreenUpdating = False
Dim Area As Range, sr As Long, er As Long
For Each Area In Range("A2", Range("G" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
  With Area
    sr = .Row
    er = sr + .Rows.Count - 1
    Range("A" & sr & ":G" & er).Sort key1:=Range("G" & sr), order1:=1
  End With
Next Area
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hello Hiker95,
Many many thanks for that code it works perfectly.Could I possibly ask you another question.I have another spreadsheet that works in a similar way to this one i.e the records are split up into groups.,and it documents students maths marks over a given time.Here is an example.What I would like to do is add another column(perhaps bewteen week and score columns)
that gives the position of the student in that record

nameyearlevelweekscore
jill evans521056
jill evans521149
jill evans521277
jill evans521384
emptyemptyemptyemptyempty
john ellis 341045
john ellis341154
john ellis341254
john ellis341329
john ellis341466
empty
empty
empty
empty
empty
phil simm461089
phil simm461176
phil simm461241
phil simm461341
phil simm461456
phil simm461559
phil simm461661
phil simm461761

<tbody>
</tbody>


to this
nameyearlevelweekpositionscore
jill evans5211149
jill evans5210256
jill evans5212377
jill evans5213484
emptyempty
empty
empty
empty
empty
john ellis3413129
john ellis 3410245
john ellis3411354
john ellis3412354
john ellis3414466
empty
empty
empty
empty
empty
empty
phil simm4612141
phil simm4613141
phil simm4614256
phil simm4615359
phil simm4616461
phil simm4617461
phil simm4611576
phil simm4610689

<tbody>
</tbody>


So the position column reflects the new position of the score after the sort.
If two scores are the same then that will be a joint position,as with John Ellis joint 4th with tow sets of 54 and phil simm who has a joint 1st and 4th.
Hope this nakes sense.Any help much appreciated
regardsZM























j
 
Upvote 0
Try this to see if it gets everything


Code:
Dim oneArea as Range

For Each oneArea in Range("A:G").SpecialCells(xlCellTypeConstants).Areas
    MsgBox oneArea.Address
Next oneArea
 
Last edited:
Upvote 0
Hello Dryver 14,
Many thanks for your reply.When I run the code it breakes down and when I debug it the following line is highlighted in yellow:
Selection.Sort key1:=Cells(i, 7), order1:=xlAscending
Does this help at all :)
Kind regards and thankyou for your reply
 
Upvote 0
Thanks for the feedback, I am still learning and get told again and again to use Columns where they are fixed it should have been Selection.Sort key1:=Cells("G" & i), order1:=xlAscending

7 was column H
 
Last edited:
Upvote 0
Yeah, I am getting muddled here cos I tested my code and it worked, I am not sure how we have ended up on G when the original request had the amounts in column H..or column 7
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,933
Members
449,134
Latest member
NickWBA

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