Split an address into component parts UDF.

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,297
Office Version
  1. 365
Platform
  1. Windows
Not a question, I just wanted to share this code.

If you have an address in a single cell, this function will split it into its component parts when entered as an array formula over however many cells you wish.

The second parameter will align the (UK) postcode to the right if set to true (defaults to false), the third parameter puts any overflow components to the left if set to false (defaults to true) and the fourth parameter is the address delimiter (default is a comma).

Any comments and improvements gladly accepted!
Code:
'//Parameters:      Target - single cell containing source address.
'//                 AlignPostCode - Places the postcode in the rightmost column of the selected range.
'//                                 False (default) = don't align postcode.
'//                 AlignOverFlow - Place overflow to left or right.  Default is right.
'//                 Delimiter     - Separates between address.  Default is a comma.

Public Function SplitAddress(Target As Range, Optional AlignPostCode As Boolean = False, _
     Optional AlignOverFlowLeft As Boolean = True, Optional Delimiter As String = ",") As Variant
    
    Dim lngRangeSize As Long, lngTargetSize As Long
    Dim strTargetValue As String
    Dim aTarget As Variant
    Dim aFinalArray As Variant
    
    Dim objRGX As Object
    Dim UKPostCode As String
    Dim colMatches As Variant
    
    Dim i As Long, j As Long, k As Long
    
    UKPostCode = "(?:(?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|" _
            & "H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|" _
            & "T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])? \d[A-Z]{2})"
            
    lngRangeSize = Application.Caller.Count
    strTargetValue = Replace(Replace(Trim(Replace(Replace(Target.Value, Chr$(13), _
        ""), Chr$(10), Delimiter)), Delimiter & " ", Delimiter), Delimiter & Delimiter, Delimiter)
    
    ReDim aFinalArray(1 To lngRangeSize)
    
    If AlignPostCode Then
        Set objRGX = CreateObject("VBScript.RegExp")
        With objRGX
            .Pattern = UKPostCode
            .Global = True
            Set colMatches = .Execute(strTargetValue)
        End With
        
        If colMatches.Count Then
            '//Move the postcode to the end of the string.
            strTargetValue = Replace(Replace(strTargetValue, colMatches(0), "") & _
                Delimiter & colMatches(0), Delimiter & Delimiter, Delimiter)
        End If
    End If
    aTarget = Split(strTargetValue, Delimiter)
    lngTargetSize = UBound(aTarget) + 1
    If AlignPostCode Then
        If colMatches.Count Then
            aFinalArray(lngRangeSize) = aTarget(UBound(aTarget))
            lngRangeSize = lngRangeSize - 1
            lngTargetSize = lngTargetSize - 1
        End If
    End If
    If lngTargetSize - lngRangeSize <= 0 Then
        '//The final array size is larger than the passed array.
        '//Everything will fit with room to spare.
        j = 0
        For i = 1 To lngRangeSize
            If j < lngTargetSize Then
                aFinalArray(i) = aTarget(j)
            Else
                aFinalArray(i) = ""
            End If
            j = j + 1
        Next i
    Else
        '//The final array size is smaller than the passed array.
        If Not AlignOverFlowLeft Then
            '//The excess passed elements need to be placed to the left.
            j = lngRangeSize
            For i = lngTargetSize - 1 To 0 Step -1
                If j > 0 Then
                    aFinalArray(j) = aTarget(i)
                    j = j - 1
                Else
                    aFinalArray(1) = aTarget(i) & Delimiter & " " & aFinalArray(1)
                End If
            Next i
        Else
            '//The excess passed elements need to be placed to the right.
            j = 1
            For i = 0 To lngTargetSize - 1
                If j <= lngRangeSize Then
                    aFinalArray(j) = aTarget(i)
                    j = j + 1
                Else
                    aFinalArray(j - 1) = aFinalArray(j - 1) & Delimiter & aTarget(i)
                End If
            Next i
        End If
    End If
    
    SplitAddress = aFinalArray
    
End Function

Darren.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Delmar D'Percy,

Can you post screenshots with example addresses?

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Colo's HTML Maker.
http://www.puremis.net/excel/downloads.shtml

Instructions for using "Colo's HTML Maker":
http://www.mrexcel.com/forum/showthread.php?t=89356


Or, with Excel Jeanie HTML 4.
http://www.excel-jeanie-html.de/index.php?f=1

Instructions for using "Excel Jeanie HTML 4":
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php


Have a great day,
Stan
 
Upvote 0
Certainly.
As you can see below, the first example is just splitting the address with the overflow on the right - Essex & Postcode in the same cell.
The second example is placing the postcode on its own, so the overflow appears in the next cell along.
The third example is the same, but with the overflow on the left.
The fourth is the same as the third, but with the country added.
The fifth is just a straight split - same number of components as cells selected.
The sixth is aligning the postcode to the right over five cells, so a gap is left.
Postcode.xls
ABCDEF
124ExampleStreet,Chelmsford,Essex,CM32ET24ExampleStreetChelmsfordEssex,CM32ET
225ExampleStreet,Chelmsford,Essex,CM32ET25ExampleStreetChelmsford,EssexCM32ET
326ExampleStreet,Chelmsford,Essex,CM32ET26ExampleStreet,ChelmsfordEssexCM32ET
427ExampleStreet,Chelmsford,Essex,CM32ET,England27ExampleStreet,Chelmsford,EssexEnglandCM32ET
526ExampleStreet,Chelmsford,Essex,CM32ET26ExampleStreetChelmsfordEssexCM32ET
627ExampleStreet,Chelmsford,Essex,CM32ET26ExampleStreetChelmsfordEssex CM32ET
Sheet2
 
Upvote 0
Excel Workbook
ABCDEF
124 Example Street, Chelmsford, Essex, CM3 2ET24 Example StreetChelmsfordEssex,CM3 2ET**
225 Example Street, Chelmsford, Essex, CM3 2ET25 Example StreetChelmsford,EssexCM3 2ET**
326 Example Street, Chelmsford, Essex, CM3 2ET26 Example Street, ChelmsfordEssexCM3 2ET**
427 Example Street, Chelmsford, Essex, CM3 2ET, England27 Example Street, Chelmsford, EssexEnglandCM3 2ET**
528 Example Street, Chelmsford, Essex, CM3 2ET28 Example StreetChelmsfordEssexCM3 2ET*
629 Example Street, Chelmsford, Essex, CM3 2ET28 Example StreetChelmsfordEssex*CM3 2ET
730 Example Street,Chelmsford,Essex,CM3 2ET30 Example StreetChelmsford,EssexCM3 2ET**
829 Example Street~Chelmsford~Essex~CM3 2ET29 Example StreetChelmsford~EssexCM3 2ET**
Sheet2
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,673
Members
449,463
Latest member
Jojomen56

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