G Singh
G Singh

Reputation: 11

How to get last item in white space separated words in cell?

For example I have 500+ rows of data that is formatted as example: Hamilton Washington 30 OR 12345

The issue is the last item in the cell is a zip code and I need to copy it into another cell. The number of items in each cell is not the same so split text leaves it in different columns.

I essentially want the "12345" or last item in the cell since for all of them it will be the zip code.

Thanks

Upvotes: 1

Views: 2244

Answers (2)

Erik Tyler
Erik Tyler

Reputation: 9355

Suppose that your original string data is in A2:A. You could place the following formula in B2 (assuming B2:B is empty first):

=ArrayFormula(IF(A2:A="",,REGEXEXTRACT(A2:A,"\s([\S]+)$")))

This means "If a cell in A2:A is blank, leave the corresponding cell in B2:B blank; otherwise, return the last group of non-spaces that follows a space and ends with the end of the string."

Upvotes: 1

SputnikDrunk2
SputnikDrunk2

Reputation: 4048

You can try this function below & adjust it to your own setup:

=ARRAYFORMULA(TRIM(RIGHT(SUBSTITUTE(A1:A," ",REPT(" ",10)),10)))

Sample Result:

enter image description here

Reference:

Extract the Last Word In Excel & Google Sheets

TRIM

RIGHT

SUBTITUTE

REPT

Upvotes: 1

Related Questions