Concatenate cells, ignoring blanks, then trim

dunlopoil

Board Regular
Joined
May 29, 2008
Messages
92
=IF(Z2="","",Z2&","&IF(AA2="","",AA2&",")&IF(AB2="","",AB2&",")&IF(AC2="","",AC2&","))

Hi,
I'm using the above formula to join text from columns Z through AC, separated by a comma. I now want to remove the comma at the end of the new string. Also, I would prefer it if the four cells were separated by a slash rather than a comma, but when I simply replace the commas in the formula with slashes I get an error.

Can anyone help?

Many thanks,
Doug.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Replacing ALL the commas with a slash would definately give you errors...

This is how you could replace the commas with a slash, and get rid of the comma(or slash) at the end:

=IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2))
 
Upvote 0
Replacing ALL the commas with a slash would definately give you errors...

This is how you could replace the commas with a slash, and get rid of the comma(or slash) at the end:

=IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2))

Thanks Keith. That works as far as replacing commas with slashes but I'm still left with a slash at the end of the string. Any ideas?
 
Upvote 0
=substitute(trim(z2&" "&aa2&" "&ab2&" "&ac2)," ","/")

Mike,
Thanks for that. It's not quite what I'm looking for as it puts a slash between each word (e.g. if the entry in Z2 is red apples it appears as red/apples). I want to separate each cell entry, not each word.
 
Last edited:
Upvote 0
Try this one out

=if(right(IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2)),1)="/",left(IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2)),len(IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2)))-1),IF(Z2="","",Z2&"/"&IF(AA2="","",AA2&"/")&IF(AB2="","",AB2&"/")&IF(AC2="","",AC2)))

It checks to see if the last character is a "/"... if it is, it gives you everything up to that "/"
 
Upvote 0
Try like this

=SUBSTITUTE(IF(Z2="","","/"&Z2)&IF(AA2="","","/"&AA2)&IF(AB2="","","/"&AB2)&IF(AC2="","","/"&AC2),"/","",1)
 
Upvote 0
This is messy, but it should work
=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(Z2," ",CHAR(5))&" "&SUBSTITUTE(AA2," ",CHAR(5))&" "&SUBSTITUTE(AB2," ",CHAR(5))&" "&SUBSTITUTE(AC2," ",CHAR(5)))," ","/"),CHAR(5)," ")

After simulposting: Barry's is much cleaner.
 
Upvote 0

Forum statistics

Threads
1,216,111
Messages
6,128,898
Members
449,477
Latest member
panjongshing

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