VBA How to Sort multiple Ranges of Data

lepage0

New Member
Joined
Mar 21, 2016
Messages
11
Good day,

I am looking for a solution to my issue. Cannot figure it out to save my life.

What I have is a long list of data but seperated by blank rows. For each Block of data (EX rows 4:8) needs to be sorted by Column "I" largest to smallest.

THere are 3-6 blocks of data. Again, all seperated by an empty row.

I have this code below that works perfectly with the sorting, however - What is hapening is that its actually skipping all the blank rows and jumps straight to literally the last row of all my data, sorting it, and bunching it all up. Which cannot happen. Needs to be separated. See code Below.

Code:
Sub Sort()


Dim Lastrow As Long

Lastrow = Cells(Rows.Count, 2).End(xlUp).Row
    Range("A4:K" & Lastrow).Sort key1:=Range("I4:I" & Lastrow), _
        order1:=xlDescending, Header:=xlNo

End Sub

What this code is doing is choosing all the Data from "A4" to "K and blank row" and then sorting Row "I" in Descending Order.


See example of Data - from the top row to where there is a blank row i need that chunk of data sorted by column "I"

A B C D E F G H I
1587:13:544:53:372:451:5262.9%12:09:2651.7%2:451:52
7:28:32:30:204:054:202.8%0:58:554.2%4:054:20
1:02:54:03:522:543:520.4%0:06:460.5%2:543:52
2:04:43:03:382:221:490.8%0:08:220.6%2:221:49
4:30:01:23:277:305:521.6%0:53:283.8%7:305:52
1:06:48:04:216:484:210.4%0:11:090.8%6:484:21
3:09:53:04:513:181:371.2%0:14:451.0%3:181:37
3:14:39:07:424:532:341.2%0:22:211.6%4:532:34
5:11:14:10:142:152:032.0%0:21:301.5%2:152:03
1:03:43:01:113:431:110.4%0:04:540.3%3:431:11
1:02:12:04:172:124:170.4%0:06:290.5%2:124:17
1:03:53:06:253:536:250.4%0:10:180.7%3:536:25
13:44:01:49:453:233:505.2%1:33:496.7%3:233:50
1:01:36:02:341:362:340.4%0:04:100.3%1:362:34
1:03:57:07:183:577:180.4%0:11:150.8%3:577:18
1:05:40:00:345:400:340.4%0:06:140.4%5:400:34
2:05:19:03:422:401:510.8%0:09:020.6%2:401:51
211:19:132:10:343:466:138.4%3:29:3914.9%3:466:13
7:18:36:19:012:392:432.8%0:37:342.7%2:392:43
1:01:19:00:051:190:050.4%0:01:240.1%1:190:05
8:07:47:05:210:580:403.2%0:13:040.9%0:580:40
1:07:41:01:507:411:500.4%0:09:310.7%7:411:50
1:04:33:01:564:331:560.4%0:06:290.5%4:331:56
5:26:50:07:505:221:342.0%0:34:402.5%5:221:34
1:09:28:00:559:280:550.4%0:10:230.7%9:280:55
1:12:16:01:5512:161:550.4%0:14:111.0%12:161:55

<tbody>
</tbody><colgroup><col><col><col><col><col><col><col><col><col span="3"><col span="2"></colgroup>
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this:
Assuming:
-There is header in row 1
-Last column is K

Code:
Sub a930196()
Dim i As Long
Dim y As Long
Dim rr As Long

Application.ScreenUpdating = False
rr = Range("A" & Rows.count).End(xlUp).row
For i = 2 To rr - 1
  y = Range(Cells(i, "A"), Cells(rr + 1, "A")).Find("", SearchOrder:=xlByRows, LookAt:=xlWhole, _
  SearchDirection:=xlNext).row
  Range(Cells(i, "A"), Cells(y - 1, "K")).Sort key1:=Columns("I"), order1:=xlDescending, Header:=xlNo
  i = y
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi, this works really well, I need to use this but for it to only start from row 20 downwards, how do I change this code to start from row 20?
Many thanks
Andy
 
Upvote 0
Try changing this:
VBA Code:
For i = 2 To rr - 1
to
VBA Code:
For i = 20 To rr - 1
Legend, this worked a treat, thank you so much, I have one other want, not sure if possible. Would it be possible to have more than one blank line between the rows that have data? further, could the number of blank lines vary, ie Row 20-25 has data, followed by 3 blank rows, rows 29-32 has data, followed by 5 blank lines, followed by more data etc etc. Would it be possible to alter the code to cater for that scenario?? Really apprecaite your help with this.
 
Upvote 0
@Andy Mitchell
Here's an example:
1. Start at row 2
2. Data is in col A:D
3. Sort descending by col C

Please, read the comments on how to change the values as needed.
VBA Code:
Sub Andy_Mitchell()

Dim c As Range, f As Range

Application.ScreenUpdating = False
'"A2" means start at A2
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    With c.Resize(, 4) 'expand to col A:D, 4 is col D
        'sort descending by col 3, i.e col C
        .Sort key1:=.Columns(3), order1:=xlDescending, Header:=xlNo
    End With
Next
Application.ScreenUpdating = True

End Sub
BEFORE
Book2
ABCD
2Jeremiah1FMario
3Giovanni2OAdrien
4Owen3ZManuel
5
6Marvin5JPaxton
7Clayton6XJoe
8
9
10
11Iker10UBoone
12Blaise11BZein
13Ariel12WPatrick
14
15
16Rex16NKellen
17Ruben17MClyde
18Aydin18KMark
Sheet2


AFTER
Book2
ABCD
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6Clayton6XJoe
7Marvin5JPaxton
8
9
10
11Ariel12WPatrick
12Iker10UBoone
13Blaise11BZein
14
15
16Rex16NKellen
17Ruben17MClyde
18Aydin18KMark
Sheet2
 
Upvote 0
@Andy Mitchell
Here's an example:
1. Start at row 2
2. Data is in col A:D
3. Sort descending by col C

Please, read the comments on how to change the values as needed.
VBA Code:
Sub Andy_Mitchell()

Dim c As Range, f As Range

Application.ScreenUpdating = False
'"A2" means start at A2
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)

For Each c In f.Areas
    With c.Resize(, 4) 'expand to col A:D, 4 is col D
        'sort descending by col 3, i.e col C
        .Sort key1:=.Columns(3), order1:=xlDescending, Header:=xlNo
    End With
Next
Application.ScreenUpdating = True

End Sub
BEFORE
Book2
ABCD
2Jeremiah1FMario
3Giovanni2OAdrien
4Owen3ZManuel
5
6Marvin5JPaxton
7Clayton6XJoe
8
9
10
11Iker10UBoone
12Blaise11BZein
13Ariel12WPatrick
14
15
16Rex16NKellen
17Ruben17MClyde
18Aydin18KMark
Sheet2


AFTER
Book2
ABCD
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6Clayton6XJoe
7Marvin5JPaxton
8
9
10
11Ariel12WPatrick
12Iker10UBoone
13Blaise11BZein
14
15
16Rex16NKellen
17Ruben17MClyde
18Aydin18KMark
Sheet2
Looks like just what I need, thank you again, one final request, I now know which row to get it to start on, could you tell me where to change it so it only works from row 20 to row 99, anything below row 99 it wont touch. This thread I'm sure will help lots of others as well. Thanks again and is certainly helping me with my vb education.
 
Upvote 0
could you tell me where to change it so it only works from row 20 to row 99, anything below row 99 it wont touch
Try changing this:
VBA Code:
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)
to:
VBA Code:
Set f = Range("A20:A99").SpecialCells(xlCellTypeConstants)

And one thing I should mention earlier:
I assume we can use col A to determine if a row is empty, that is, if a cell in col A is empty then the entire row must be empty.
 
Upvote 0
Try changing this:
VBA Code:
Set f = Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants)
to:
VBA Code:
Set f = Range("A20:A99").SpecialCells(xlCellTypeConstants)

And one thing I should mention earlier:
I assume we can use col A to determine if a row is empty, that is, if a cell in col A is empty then the entire row must be empty.
Perfect, perfect, perfect, thank you so much, all good, I have learnt a lot from this one thread, all the best
 
Upvote 0
Tested and all works, I think what would complete this thread would be able to have 2 sort columns, Column B and column D. Column B could have multiple 1's for example and then in column D you might want to sort names alphabetically. What code would need changing to be able to do more than one sort column? Many thanks
 
Upvote 0

Forum statistics

Threads
1,215,460
Messages
6,124,949
Members
449,198
Latest member
MhammadishaqKhan

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