VBA Sort Street Address Without Helper Column

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,180
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good Morning,

I am trying to sort street addresses. But what I would like to do is not use a helper column to get this done. Can anyone please help me out with some code that will help with this! Thanks in advance.

PS the street addresses are cleaned up so they will always start with a number followed by the street name.

Thanks in advance!

Book1
ABCDEF
1Student #Last NameFirst NameSchoolGrStreet Address
2100xxxxxxxxxxxxxxxxxxxx2 Acorn Drive
3101xxxxxxxxxxxxxxxxxxxx10 Kobe St
4102xxxxxxxxxxxxxxxxxxxx1020 Madison Ave
5103xxxxxxxxxxxxxxxxxxxx1952 East Main St
6104xxxxxxxxxxxxxxxxxxxx3883 Jones Ave
7105xxxxxxxxxxxxxxxxxxxx4 Kent Court
8106xxxxxxxxxxxxxxxxxxxx41 Main Street
9107xxxxxxxxxxxxxxxxxxxx423 Barnum Ave
10108xxxxxxxxxxxxxxxxxxxx5 Alba Ave
11109xxxxxxxxxxxxxxxxxxxx67 State St
12110xxxxxxxxxxxxxxxxxxxx22 Kent Court
Sheet1





To this.

Book1
ABCDEF
1Student #Last NameFirst NameSchoolGrStreet Address
2100xxxxxxxxxxxxxxxxxxxx2 Acorn Drive
3108xxxxxxxxxxxxxxxxxxxx5 Alba Ave
4107xxxxxxxxxxxxxxxxxxxx423 Barnum Ave
5103xxxxxxxxxxxxxxxxxxxx1952 East Main St
6104xxxxxxxxxxxxxxxxxxxx3883 Jones Ave
7105xxxxxxxxxxxxxxxxxxxx4 Kent Court
8110xxxxxxxxxxxxxxxxxxxx22 Kent Court
9101xxxxxxxxxxxxxxxxxxxx10 Kobe St
10102xxxxxxxxxxxxxxxxxxxx1020 Madison Ave
11106xxxxxxxxxxxxxxxxxxxx41 Main Street
12109xxxxxxxxxxxxxxxxxxxx67 State St
Sheet2
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Without a helper column, it is going to sort by the whole value, including the numbers. I don't think there is any way to tell Excel's Sort functionality to ignore part of the value for a single cell in sorting.

Perhaps you could split your street column into two column?
One for the street number, and one for the street name. That would then allow you to sort like you want it to.
 
Upvote 0
Try this:-
Results start "G1".
Code:
[COLOR="Navy"]Sub[/COLOR] MG20Jul11
[COLOR="Navy"]Dim[/COLOR] Rng     [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Ray     [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] i       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] j       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] itxt    [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] jtxt    [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] temp(1 To 6) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Ray = Rng.Resize(, 6).Value
[COLOR="Navy"]For[/COLOR] i = 1 To UBound(Ray)
    [COLOR="Navy"]For[/COLOR] j = i To UBound(Ray)
        itxt = Mid(Ray(i, 6), InStr(Ray(i, 6), " ") + 1, Len(Ray(i, 6)) - InStr(Ray(i, 6), " ") + 1)
        jtxt = Mid(Ray(j, 6), InStr(Ray(j, 6), " ") + 1, Len(Ray(j, 6)) - InStr(Ray(j, 6), " ") + 1)
        [COLOR="Navy"]If[/COLOR] jtxt < itxt [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]For[/COLOR] n = 1 To 6
                temp(n) = Ray(i, n)
                Ray(i, n) = Ray(j, n)
                Ray(j, n) = temp(n)
            [COLOR="Navy"]Next[/COLOR] n
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] j
[COLOR="Navy"]Next[/COLOR] i
Range("G1").Resize(UBound(Ray, 1), UBound(Ray, 2)) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Yes, I should have qualified my statement to say I don't think it can be done "without the use of VBA", as I don't think you can change how the native Excel Sort functionality works.

Basically, you would need to create your own sorter in VBA, like Mick has done (unless you could split the column like I mentioned, or use a helper column, which you do not want to do).
 
Upvote 0
Mick you are right on the money! It works great! Thank you again! Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

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