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
 
How about
Code:
Sub rankandSort()
   
   Dim Rng As Range
   Dim Ar As Areas
   
   Set Ar = Range("A2", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
   Columns(5).Insert
   Range("E1").Value = "Position"
   
   For Each Rng In Ar
      Rng.Sort key1:=Range("F:F"), order1:=1
      With Intersect(Rng, Range("E:E"))
         .FormulaArray = "=rank(" & .Offset(, 1).Address & "," & .Offset(, 1).Address & ",1)"
         .Value = .Value
      End With
   Next Rng
End Sub
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Re: Looping and sorting through blocks of rows
Hello Hiker95,
Many many thanks for that code it works perfectly.

zeekmcphee,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Hi Fluff
Thanyou for that reply :)
When I run the code it inserts Position into column E and pushes score into column F, the sort in column F works great but then every cell in the newly created Position column gets filled with what seems to be a formula:

=RANK(R2C6,R5C6,R2C6:R5C6,1)

which iterates as it goes through the cells
Any suggestions are welcome
Kind Regards
ZM
 
Upvote 0
It puts a formula into col E but then this line
Code:
 .Value = .Value
Should then convert that formula into values. Is that not happening?
 
Upvote 0
Hi Fluff,
Sorry My fault:rolleyes: I just realised that my "score"field was was text,once I converted that into a number the program works perfectly.
Many many thanks for that great bit of coding :) :)

Kind Regards
ZM
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Hi Dryver
Thats probably my fault I have been messing about with half a dozen sheets and getting a bit mixed up.:confused:,But much thanks
for your help
Kind Regards
ZM
 
Upvote 0
Following on from my original 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 ellis341045
john ellis341154
john ellis341254
john ellis341329
john ellis341466
emptyemptyemptyemptyempty
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
emptyemptyemptyemptyemptyempty
john ellis3413129
john ellis3410245
john ellis3411354
john ellis3412354
john ellis3414466
emptyemptyemptyemptyemptyempty
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
 
Upvote 0
In what way is this different to post#4?
 
Upvote 0
Hi Fluff,

Both questions are more or less identical,nobody seemed to pick up on it earlier so I reproduced it here .
kind regards
zm
 
Upvote 0

Forum statistics

Threads
1,215,269
Messages
6,123,976
Members
449,138
Latest member
abdahsankhan

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