Formula to Separate City from Address

TaraAlexander

New Member
Joined
Jun 6, 2018
Messages
4
I have a list that looks like:

Address
1820 E River Rd Ste 230Tucson
7550 E Mcdonald Dr Ste AScottsdale
442 Kaleimamahu StHonolulu
6 Larch Ave Ste 398Wilmington
85 Willow StNew Haven
64 Thompson St Ste A105East Haven
PO BOX 2590416 Slater RoadNew Britain
7059 Blair Rd Nw Ste 300Washington
1018 Bienville StNew Orleans
11131 N Oak Hills PkwyBaton Rouge
1304 Business Park DrTraverse City
1118 W Front StMonroe
121 Hunter Ave Ste 205Saint Louis

<tbody>
</tbody>
open

Need a formula to make it look like:

AddressAddress 1City
1820 E River Rd Ste 230Tucson1820 E River Rd Ste 230Tucson
7550 E Mcdonald Dr Ste AScottsdale7550 E Mcdonald Dr Ste AScottsdale
442 Kaleimamahu StHonolulu442 Kaleimamahu StHonolulu
6 Larch Ave Ste 398Wilmington6 Larch Ave Ste 398Wilmington
85 Willow StNew Haven85 Willow StNew Haven
64 Thompson St Ste A105East Haven64 Thompson St Ste A105East Haven
PO BOX 2590416 Slater RoadNew BritainPO BOX 2590416 Slater RoadNew Britain
7059 Blair Rd Nw Ste 300Washington7059 Blair Rd Nw Ste 300Washington
1018 Bienville StNew Orleans1018 Bienville StNew Orleans
11131 N Oak Hills PkwyBaton Rouge11131 N Oak Hills PkwyBaton Rouge
1304 Business Park DrTraverse City1304 Business Park DrTraverse City
1118 W Front StMonroe1118 W Front StMonroe
121 Hunter Ave Ste 205Saint Louis121 Hunter Ave Ste 205Saint Louis

<tbody>
</tbody>
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Welcome to the Board!

This can get a bit tricky, because of all the different possibilities. I don't know how to do it with native Excel functions, but can develop a User Defined Function in VBA that will handle most cases (works for all the examples that you posted). You just simply paste this code in a new Excel module in the VB Editor, and then use it like any other Excel function.

Here is the code:

Code:
Function GetCity(myAddress As String) As String


    Dim ln As Long
    Dim i As Long
    Dim curCap As Boolean
    Dim prvCap As Boolean
    Dim myAsc As Byte
    
'   Find length of string
    ln = Len(myAddress)
    
    curCap = False
    prvCap = False
'   If string is not empty, do the following
    If ln > 0 Then
'       Loop through string backwards
        For i = ln To 1 Step -1
'           Set previous value equal to current
            prvCap = curCap
'           Get current value
            myAsc = Asc(Mid(myAddress, i, 1))
            If myAsc >= 65 And myAsc <= 90 Then
                curCap = True
            Else
                curCap = False
            End If
'           Check to see if previous is caps and current is not a space
            If (prvCap = True) And (myAsc <> 32) Then
                GetCity = Mid(myAddress, i + 1, ln)
                Exit Function
            End If
        Next i
    End If
    
End Function
So, then if your first entry is in cell A1, then to get the city in cell C1, enter this formula in cell C1:
=GetCity(A1)

Then to get the rest of the address in cell B1, use this formula in that cell:
=SUBSTITUTE(A1,C1,"")
 
Upvote 0
Here is another macro for you to consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetCity()
  Dim R As Long, X As Long, Arr As Variant
  Arr = Range("A2", Cells(Rows.Count, "A").End(xlUp).Offset(, 1))
  For R = 1 To UBound(Arr)
    For X = Len(Arr(R, 1)) - 1 To 1 Step -1
      If Mid(Arr(R, 1), X, 2) Like "[! ][A-Z]" Then
        Arr(R, 2) = Mid(Arr(R, 1), X + 1)
        Arr(R, 1) = Left(Arr(R, 1), X)
        Exit For
      End If
    Next
  Next
  Range("B2:C2").Resize(UBound(Arr)) = Arr
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,873
Members
449,130
Latest member
lolasmith

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