Formula to convert text based on commas

Nola111

Board Regular
Joined
Aug 28, 2013
Messages
119
I'm trying to use a formula which would take what's in the column A and convert it to what's in column B. Basically, the entire contents would end up surrounded by square brackets, and anywhere there is a comma, this comma should act as a separator, and finally all phrases separated by commas should be enclosed in parentheses. It would look like this:

AB
1
Advocate Agriculturist, 1996 - 2001 Union Councilor, Mangalur Union, District Secretary (A.I.A.D.M.K.) from 2004 : Cuddalore District, Tamil Nadu, Sports, Games and Tourism

<tbody>
</tbody>
["Advocate Agriculturist, 1996 - 2001 Union Councilor", "Mangalur Union", "District Secretary (A.I.A.D.M.K.) from 2004 : Cuddalore District", "Tamil Nadu", "Sports", "Games and Tourism"]

<tbody>
</tbody>
2
Agriculturist Businessperson, Builder, 1990-95 & 95-97 Corporator, Parola Nagarpalika (Two Terms), , Agriculture and defence affairs

<tbody>
</tbody>
["Agriculturist Businessperson", "Builder", "1990-95 & 95-97 Corporator", "Parola Nagarpalika (Two Terms)", "Agriculture and defence affairs"]

<tbody>
</tbody>
3
Social Worker Agriculturist, Educationist, May, 2014 Elected to 16 th Lok Sabha, , Agriculture and rural development, human resources management, education, sports, international trade and economics, E-governance and strategy, promotion of primary education

<tbody>
</tbody>
["Social Worker Agriculturist", "Educationist", "May", "2014 Elected to 16 th Lok Sabha", "Agriculture and rural development", "human resources management", "education", "sports", "international trade and economics", "E-governance and strategy", "promotion of primary education"]

<tbody>
</tbody>
4
President of Istanbul

<tbody>
</tbody>
["President of Istanbul"]

<tbody>
</tbody>

<tbody>
</tbody>

Note in row 4, if there are no commas, we would just want the phrase in quotes and brackets.

How would I accomplish this with a formula? Is this done with some combination of Substitute and Trim?

Thanks!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
This will work for your examples:

="["""&SUBSTITUTE(SUBSTITUTE(A1,", ,",","),", ",""", """)&"""]"
 
Upvote 0
This will work for your examples:

="["""&SUBSTITUTE(SUBSTITUTE(A1,", ,",","),", ",""", """)&"""]"

Thanks Steve!

One more question - how would the formula change if I wanted to remove the space after each comma in column B? I tried tinkering with your formula but just kept screwing it up. I'm too much a novice at these formulas, unfortunately.
 
Last edited:
Upvote 0
If you look at the formula, the last comma in it has a space after it. Remove that space.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top