×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Stringing together with IF()

## Stringing together with IF()

(OP)
I have 5 cells (but it could be more) which may or may not have a string in them.

I want to generate a string from them that looks like:

"cell1_cell2_cell3_cell4_cell5", but if cell2 is empty then it should be "cell1_cell3_cell4_cell5", if cell4 and cell5 are empty it should be "cell1_cell2_cell3" and so on.

I tried this:
=IF($C$10<>"",IF($D$10<>"",$C$10&$E$7,$C$10),""))
&(IF($D$10<>"",IF($E$10<>"",$D$10&$E$7,$D$10),""))
&(IF($E$10<>"",IF($F$10<>"",$E$10&$E$7,$E$10),""))
&(IF($F$10<>"",IF($G$10<>"",$F$10&$E$7,$F$10),""))
&(IF($G$10<>"",$G$10,""))

($c$10 is cell1, etc)

but then realized if cell2 through 4 are empty this won't work, and I would have to nest a whole bunch more if statements.  Is there a better way to do this?

Thanks,
Jeff

### RE: Stringing together with IF()

Try using the formula:
=CONCATENATE(C10,D10,E10,F10,G10)

Mala Singh
'Dare to Imagine'

### RE: Stringing together with IF()

(OP)
But I need to put the underscores in conditionally.  This will just group all of the strings together as "cell1cell2cell3cell4cell5"

Jeff

### RE: Stringing together with IF()

You can concatenate up to 30 cells (I do not think yu will need to do it for more).

If you need the result to show the cell values separated with underscores, the following would be a better way:

=C10&IF(LEN(C10)>0,"_","")&D10&IF(LEN(D10)>0,"_","")&E10&IF(LEN(E10)>0,"_","")&F10&IF(LEN(F10)>0,"_","")&G10

Mala Singh
'Dare to Imagine'

### RE: Stringing together with IF()

Almost like chatting on the forum.
:)

Mala Singh
'Dare to Imagine'

### RE: Stringing together with IF()

Might be a bit cleaner to define your own function in VBA and use a CASE statement.

### RE: Stringing together with IF()

(OP)
I added another LEN statement so that it doesn't have a trailing "_" if G10 is empty - and voila!

Thanks Mala!

(VBA is not my friend)

Jeff

### RE: Stringing together with IF()

Yes, a user defined function would be much cleaner.
Added advantage: You would not have to type such a long formula everytime.

You could use the following code

#### CODE

Function JoinCells(cell1 As Range, ParamArray Addr()) As String
JoinCells = cell1.Text
If Len(JoinCells) > 0 Then JoinCells = JoinCells & "_"
Txt1 = ""
Txt1 = cell.Text
If Txt1 <> "" Then JoinCells = JoinCells & Txt1 & "_"
Next cell
If Right(JoinCells, 1) = "_" Then JoinCells = Left(JoinCells, Len(JoinCells) - 1)
End Function

The formula you would enter in the cell would look like
=JoinCells(C10,D10,E10,F10,G10)

You do not have to type the individual cell refs, you can simply point to them in turn with the ctrl key depressed.

With slight modification, the code could be made to work on a continuouus range - like JoinCells(C10:G10)

Mala Singh
'Dare to Imagine'

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!