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:
to have 2 sort columns, Column B and column D.

Try:
VBA Code:
Sub Andy_Mitchell_2()

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 2, then  sort ascending by col 4
        .Sort Key1:=.Columns(2), Order1:=xlDescending, Key2:=.Columns(4), Order2:=xlAscending, Header:=xlNo
    End With
Next
Application.ScreenUpdating = True

End Sub

Before:
Book1
ABCD
1
2Jeremiah1FMario
3Giovanni2OAdrien
4Owen1ZManuel
5
6
7Iker1UBoone
8Blaise1BZein
9Ariel5WPatrick
10Rex1NKellen
11
12
13Ruben17MClyde
14Aydin18KMark
Sheet3


After:
Book1
ABCD
1
2Giovanni2OAdrien
3Owen1ZManuel
4Jeremiah1FMario
5
6
7Ariel5WPatrick
8Iker1UBoone
9Rex1NKellen
10Blaise1BZein
11
12
13Aydin18KMark
14Ruben17MClyde
Sheet3
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Fantastic, works perfectly, I have way better understanding how to manipulate my data now. Thank you for your time and helping me :)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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