Help to sort road addresses - Complex Sort Scenario

mazher

Active Member
Joined
Nov 26, 2003
Messages
355
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi All Excel Gurus,

I have address in one column as follows

1 ABC Road
1a ABC Road
10 ABC Road
11 ABC Road
11a ABC Road
2 ABC Road
3 ABC Road
6 ABC Road
7 ABC Road
52 XYZ Road
The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3, 35 BCD Road
51 BCD Road
21 ABC Road
Flat 1 23 ABC Road
5 Court PQR Road
2 Court PQR Road
7 Court PQR Road
2a Court PQR Road


I need them sorted in the road order as follows
1 ABC Road
1a ABC Road
2 ABC Road
3 ABC Road
6 ABC Road
7 ABC Road
10 ABC Road
11 ABC Road
11a ABC Road
21 ABC Road
Flat 1 23 ABC Road
The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3 35 BCD Road
51 BCD Road
2 Court PQR Road
2a Court PQR Road
5 Court PQR Road
7 Court PQR Road
52 XYZ Road


Please can some one help me either with the formula approach or with VBA.

I will be extremely thankful for that, as its driving me crazy.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:
The code use col C & D as temporary helper column (they're deleted in the end of the code), you may change that.

Code:
[FONT=lucida console][color=Royalblue]Sub[/color] a1086751a()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1086751-help-sort-road-addresses-complex-sort-scenario.html[/color][/i]
[color=Royalblue]Dim[/color] i [color=Royalblue]As[/color] [color=Royalblue]Long[/color], j [color=Royalblue]As[/color] [color=Royalblue]Long[/color], n [color=Royalblue]As[/color] [color=Royalblue]Long[/color]
[color=Royalblue]Dim[/color] tx [color=Royalblue]As[/color] [color=Royalblue]String[/color]
[color=Royalblue]Dim[/color] va, vb, x, q

Application.ScreenUpdating = [color=Royalblue]False[/color]
n = Range([color=brown]"A"[/color] & Rows.count).[color=Royalblue]End[/color](xlUp).Row
va = Range([color=brown]"A1:A"[/color] & n)
[color=Royalblue]ReDim[/color] vb([color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color]), [color=crimson]1[/color] [color=Royalblue]To[/color] [color=crimson]1[/color])
[color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color])
x = Split(va(i, [color=crimson]1[/color]), [color=brown]" "[/color])
vb(i, [color=crimson]1[/color]) = x(UBound(x) - [color=crimson]1[/color])

[color=Royalblue]Next[/color]

Range([color=brown]"C1"[/color]).Resize(UBound(vb, [color=crimson]1[/color]), [color=crimson]1[/color]) = vb
[color=Royalblue]ReDim[/color] vb([color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color]), [color=crimson]1[/color] [color=Royalblue]To[/color] [color=crimson]1[/color])

[color=Royalblue]For[/color] i = [color=crimson]1[/color] [color=Royalblue]To[/color] UBound(va, [color=crimson]1[/color])
z = va(i, [color=crimson]1[/color])

[color=Royalblue]If[/color] [color=Royalblue]Not[/color] IsNumeric(Left(z, [color=crimson]1[/color])) [color=Royalblue]Then[/color]
    vb(i, [color=crimson]1[/color]) = z
    [color=Royalblue]Else[/color]
        q = Split(z, [color=brown]" "[/color])([color=crimson]0[/color])
        [color=Royalblue]If[/color] IsNumeric(Right(q, [color=crimson]1[/color])) [color=Royalblue]Then[/color]
            vb(i, [color=crimson]1[/color]) = q
            [color=Royalblue]Else[/color]
            vb(i, [color=crimson]1[/color]) = Left(q, Len(q) - [color=crimson]1[/color])
        [color=Royalblue]End[/color] [color=Royalblue]If[/color]
[color=Royalblue]End[/color] [color=Royalblue]If[/color]

[color=Royalblue]Next[/color]

Range([color=brown]"D1"[/color]).Resize(UBound(vb, [color=crimson]1[/color]), [color=crimson]1[/color]) = vb
Range([color=brown]"A1:D"[/color] & n).Sort Key1:=Range([color=brown]"C1"[/color]), order1:=xlAscending, Key2:=Range([color=brown]"D1"[/color]), order2:=xlAscending, Header:=xlNo
Range([color=brown]"C1:D"[/color] & n).ClearContents
Application.ScreenUpdating = [color=Royalblue]True[/color]
[color=Royalblue]End[/color] [color=Royalblue]Sub[/color][/FONT]

The result is a bit different from yours, check the yellow area:

Excel 2013 32 bit
A
1
1 ABC Road
2
1a ABC Road
3
2 ABC Road
4
3 ABC Road
5
6 ABC Road
6
7 ABC Road
7
10 ABC Road
8
11 ABC Road
9
11a ABC Road
10
21 ABC Road
11
Flat 1 23 ABC Road
12
51 BCD Road
13
Flat 3, 35 BCD Road
14
The Mansion , 8 BCD Road
15
The Quadrant , 8a BCD Road
16
2 Court PQR Road
17
2a Court PQR Road
18
5 Court PQR Road
19
7 Court PQR Road
20
52 XYZ Road
Sheet: Sheet4
 
Upvote 0
Its giving me complie error and I need the result like I have posted in my sample

The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3 35 BCD Road
51 BCD Road
 
Upvote 0
Do you mean using your example above gave you compile error? or you were using actual data?
 
Upvote 0
Thanks for your time and help , I tried running that again on the sample data and no compile error.

I need the yellow highlighed cells in the same way

The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3 35 BCD Road
51 BCD Road

I have lot of entries in that format

The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3, 35 BCD Road

which will take for ever to sort them in the correct order road order.
 
Upvote 0
If the number is in the middle it is rather tricky because I don't know the right "pattern" to manipulate.
Is it always true that if the number is in the middle then there must be a comma before it? And there won't be more than 1 comma in the address.
 
Upvote 0
Thanks again, I have checked my data that wherever there is comma , there is only one comma,

But I don't know how the handle these

The Mansion , 8 BCD Road
The Quadrant , 8a BCD Road
Flat 3, 35 BCD Road

Any way I am extremely thankful once again for your time and help,

I will wait might be some VBA expert will notice my request.

Thanks again Ajuini.
 
Upvote 0
You didn't answer this question:
Is it always true that if the number is in the middle then there must be a comma before it?
 
Upvote 0
Here is the sample with all scenarios
Thomas Cottage, 271A Totteridge Road
Flat 1, 145 Bowerdean Road
Flat B, 21 Totteridge Lane
Heartoak House, 21a Totteridge Lane
1 Windrush Court, Windrush Drive
3 Denewood, Totteridge Road
7 Conway House, Hicks Farm Rise
25 Chartridge House, Windrush Drive
8 Denewood, Totteridge Road
10 Denewood, Totteridge Road
20 Leas Close, High Wycombe
21 Leas Close, High Wycombe
1 Ely House, Leas Close
1 Hereford House, Leas Close
2 York House, Leas Close


I need it sorted it like this way

Flat 1, 145 Bowerdean Road
Flat B, 21 Totteridge Lane
Heartoak House, 21a Totteridge Lane
3 Denewood, Totteridge Road
8 Denewood, Totteridge Road
10 Denewood, Totteridge Road
Thomas Cottage, 271A Totteridge Road
7 Conway House, Hicks Farm Rise
1 Ely House, Leas Close
1 Hereford House, Leas Close
2 York House, Leas Close
20 Leas Close
21 Leas Close
25 Chartridge House, Windrush Drive
1 Windrush Court, Windrush Drive

Hope this helps
 
Upvote 0
Hm, it's more complex than I thought.
I don't understand the criteria for the sort.
Why is Totteridge Road above Hicks Farm Rise?
Thomas Cottage, 271A Totteridge Road
7 Conway House, Hicks Farm Rise
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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