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:

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][TABLE="width: 280"]
<tbody>[TR]
[TD="class: xl63, width: 280"]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[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 619"]
<tbody>[TR]
[TD="class: xl65, width: 619"]["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"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][TABLE="width: 280"]
<tbody>[TR]
[TD="class: xl65, width: 280"]Agriculturist Businessperson, Builder, 1990-95 & 95-97 Corporator, Parola Nagarpalika (Two Terms), , Agriculture and defence affairs[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 619"]
<tbody>[TR]
[TD="class: xl65, width: 619"]["Agriculturist Businessperson", "Builder", "1990-95 & 95-97 Corporator", "Parola Nagarpalika (Two Terms)", "Agriculture and defence affairs"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][TABLE="width: 280"]
<tbody>[TR]
[TD="class: xl65, width: 280"]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[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 619"]
<tbody>[TR]
[TD="class: xl65, width: 619"]["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"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][TABLE="width: 280"]
<tbody>[TR]
[TD="class: xl65, width: 280"]President of Istanbul[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 619"]
<tbody>[TR]
[TD="class: xl65, width: 619"]["President of Istanbul"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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,224,816
Messages
6,181,143
Members
453,021
Latest member
Justyna P

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