How to select one item in a cell value separated by characters?

I have a list of cities in this format in each cell:
Baltimore|Maryland|United States
Frederick|Maryland|United States
Chicago|Illinois|United States

I need a custom function to pull out the 2nd one in each cell.
Maryland
Maryland
Illinois
Parsing.xlsx
LVL 1
Alex CampbellAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ejgil HedegaardCommented:
Use this formula
=MID(A1,SEARCH("|",A1)+1,SEARCH("|",A1,SEARCH("|",A1)+1)-1-SEARCH("|",A1))

Or mark the items in column A and use the "Text to columns" function on the Data tab, and use | as delimiter.
Parsing.xlsx
0
Alex CampbellAuthor Commented:
Thank you for the suggestion of the formula.
I would like to see how it would work as a custom function.
That is because I am working on learning how to build custom functions.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try use a custom function like this:

Public Function SplitString(value As String, location As Integer, Optional delimiter As String = "|") As String
    Dim tmpArr() As String
    
    On Error GoTo EH
    tmpArr = Split(value, delimiter, , vbTextCompare)
    SplitString = tmpArr(location - 1)
    
    Exit Function
EH:
    SplitString = ""
End Function

Open in new window


then in your worksheet, put the formula like:

=SplitString(A1,2)

Open in new window

Parsing_b.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Alex CampbellAuthor Commented:
wonderful. Just what I was looking for
0
Alex CampbellAuthor Commented:
I don't see how to ask a related question any more.
How could the function be adapted to pass the character to be used as the separator?
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
How could the function be adapted to pass the character to be used as the separator?

try change the 3rd parameter in that function, like:

=SplitString(A1,2, "*")

which means * is the separator
0
Alex CampbellAuthor Commented:
Great, thanks!
0
Alex CampbellAuthor Commented:
Great, thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.