How to combine cells in a range only if not blank

mljohn

Board Regular
Joined
Aug 26, 2004
Messages
196
Office Version
  1. 365
Platform
  1. Windows
How do I create a formula that will combine only the cells in a range that are not blank.

I tried this formula =IF(K3="",J3,K3&", "&L3&", "&M3&", "&N3&", "&O3&", "&P3&", "&Q3&", "&R3&", "&S3&", "&T3&", "&U3)

but if K3 is not blank I get a comma for every blank cell. Example: 4T311Q, , , , , , , , , ,

How do I combine all of the non-blank cells j3:u3 ?


Thanks

Matt
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi Matt

Assuming the values in the cells don't already contain spaces you could consider using:


Excel 2010
CDEFGHIJKLMNOPQRS
2
3dogcathorsezebra
4
5
6
7dog, cat, horse, zebra
8
9
Sheet3
Cell Formulas
RangeFormula
C7=IF(K3="",J3,SUBSTITUTE(TRIM(SUBSTITUTE(K3&","&L3&","&M3&","&N3&","&O3&","&P3&","&Q3&","&R3&","&S3&","&T3&","&U3,","," "))," ",", "))
 
Upvote 0
Works great,

Can you explain how the formula works?

Especially the SUBSTITUTE(TRIM(SUBSTITUTE part

Thanks

Matt
 
Upvote 0
Sure: most of your formula remains unchanged.

The inner Substitute converts all of the commas to spaces.

The Trim then removes consecutive spaces so that only a single one remains between words/values.

The outer substitute then converts these resultant spaces back to commas.

Make sense?
 
Upvote 0
Sure: most of your formula remains unchanged.

The inner Substitute converts all of the commas to spaces.

The Trim then removes consecutive spaces so that only a single one remains between words/values.

The outer substitute then converts these resultant spaces back to commas.

Make sense?
 
Upvote 0
The easiest way to do this is this formula:

Code:
=SUBSTITUTE((TRIM(IF(C1<>"", " "&C1, "")&IF(D1<>"", " "&D1, "")&IF(E1<>"", " "&E1, "")&IF(F1<>"", " "&F1, ""))), " ", ",")

Obviously you need to adjust the cell references to meet your needs.

Basically what you have is an IF statement (with nested IF statements for each referenced cell) that will ignore empty cells (each IF is checking to make sure there's something in the referenced cell with <>"", and if there is, add a space to the front of that value in a text string), then the TRIM does its thing (removes all spaces except for a single space between the cell values, which in this case should only be the first space) and finally SUBSTITUTE changes the spaces to commas (that's the , " ", "," at the end), since you're looking for commas to delimit the values.

This is similar to what Firefly2012 posted, the difference is this formula never processes the blank cells to start with.
 
Upvote 0

Forum statistics

Threads
1,215,982
Messages
6,128,100
Members
449,420
Latest member
AussieHobbo

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