## 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

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()

=CONCATENATE(C10,D10,E10,F10,G10)

Mala Singh

'Dare to Imagine'

## RE: Stringing together with IF()

Jeff

## RE: Stringing together with IF()

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()

:)

Mala Singh

'Dare to Imagine'

## RE: Stringing together with IF()

## RE: Stringing together with IF()

Thanks Mala!

(VBA is not my friend)

Jeff

## RE: Stringing together with IF()

Added advantage: You would not have to type such a long formula everytime.

You could use the following code

## CODE

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'