Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stringing together with IF() 1

Status
Not open for further replies.

jmarkus

Mechanical
Jul 11, 2001
377
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
 
Replies continue below

Recommended for you

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


Mala Singh
'Dare to Imagine'
 
But I need to put the underscores in conditionally. This will just group all of the strings together as "cell1cell2cell3cell4cell5"

Jeff
 
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'
 
Almost like chatting on the forum.
:)



Mala Singh
'Dare to Imagine'
 
Might be a bit cleaner to define your own function in VBA and use a CASE statement.

 
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
 
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 & "_"
  For Each cell In Addr
    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'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor