Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Students Click Here

Stringing together with IF()

Stringing together with IF()

Stringing together with IF()

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:

($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?


RE: Stringing together with IF()

Try using the formula:

Mala Singh
'Dare to Imagine'

RE: Stringing together with IF()

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


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:


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

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)


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


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

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.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close