Reputation: 47
In MS Excel, I need to separate text by comma, extract three things from right and put them in three different columns i.e. city, State and Country.
Problem: In the original text which is the full address, I sometimes get only the country or only the state. Also, the length of the full address is varied.
and Here's the actual spreadsheet.
Upvotes: 0
Views: 165
Reputation: 60224
If you have Excel 2013+ with the FILTERXML
function, you can use:
=IFERROR(FILTERXML("<t><s>" & SUBSTITUTE($A1,",","</s><s>") & "</s></t>","//s[last()-3+" & COLUMNS($A:A) & "]"),"")
Fill across and down.
""
if it is not existent.Upvotes: 1
Reputation: 96753
First enter the following three user defined functions in a standard module:
Public Function LastWord(s As String) As String
Dim arr
s = ", , , , , " & s
arr = Split(s, ", ")
LastWord = arr(UBound(arr))
End Function
Public Function NLastWord(s As String) As String
Dim arr
s = ", , , , , " & s
arr = Split(s, ", ")
NLastWord = arr(UBound(arr) - 1)
End Function
Public Function NNLastWord(s As String) As String
Dim arr
s = ", , , , , " & s
arr = Split(s, ", ")
NNLastWord = arr(UBound(arr) - 2)
End Function
then in E1 enter:
=lastword(A1)
and copy downward. In D1 enter:
=NLastWord(A1)
and copy downwards. In C1 enter:
=NNLastWord(A1)
and copy downwards:
Upvotes: 0
Reputation: 3802
Assume data housed in A1:A6
as per screenshot
In C1
, formula copied across and down :
=IFERROR(TRIM(MID(SUBSTITUTE(","&$A1,",",REPT(" ",399)),(LEN($A1)-LEN(SUBSTITUTE(($A1),",",""))+COLUMN(A1)-2)*399,399)),"")
Upvotes: 3