?
Solved

Pull City out of cell

Posted on 2014-10-10
5
Medium Priority
?
47 Views
Last Modified: 2014-10-27
How do I pull out just the city of the below cell? The part in-between the commas:

28941 Canwood St, Agoura Hills, CA 91301
0
Comment
Question by:cansevin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 26

Accepted Solution

by:
ProfessorJimJam earned 2000 total points
ID: 40373582
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,",",REPT(" ",100)),200),100))
0
 
LVL 37

Expert Comment

by:bbao
ID: 40373584
the city name can be easily determined by any characters between two commas of an address. trim function is also required to retreive the final result.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40373805
Another possible solution:
=LEFT(MID(A1,FIND(",",A1)+2,99),FIND(",",MID(A1,FIND(",",A1)+2,99))-1)

-Glenn
0
 
LVL 12

Expert Comment

by:tel2
ID: 40373948
Hi bbao,

> the city name can be easily determined by any characters between two commas of an address.
Isn't that essentially what the questioner said in his question?  He said:
> The part in-between the commas
Did you read the question?

> trim function is also required to retreive the final result.
Not necessarily.  See Glenn's solution, for example.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40374442
You might want to consider a generalized parsing function, such as this one:
Public Function GetAddrPart(ByVal parmAddr, parmPart As Long) As String
    'parmAddr format: street, city, state zip
    'parmPart values
    '   1 = Street
    '   2 = City
    '   3 = State
    '   4 = Zip
    Static oRE As Object
    Static oMatches As Object
    If oRE Is Nothing Then
        Set oRE = CreateObject("vbscript.regexp")
        oRE.Global = False
        oRE.Pattern = "(\S[^,]*),\s*(\S[^,]*),\s*(\w.*?) (\d+-?\d+)"
    End If
    If oRE.test(parmAddr) Then
        Set oMatches = oRE.Execute(parmAddr)
        Select Case parmPart
            Case 1 To oMatches(0).submatches.Count
                GetAddrPart = oMatches(0).submatches(parmPart - 1)
            Case Else
                GetAddrPart = vbNullString
        End Select
    Else
        GetAddrPart = vbNullString
    End If
    
End Function

Open in new window

You can use the function in a formula, supplying which part of the parsed address you want (a value between 1 and 4)
=GetAddrPart("28941 Canwood St, Agoura Hills, CA 91301-1234",4)
=GetAddrPart(B2,4)

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question