Solved

Pull City out of cell

Posted on 2014-10-10
5
45 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 500 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 45

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

740 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