+ Reply to Thread
Results 1 to 11 of 11

How do I concatenate cells based on several if conditions?

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    How do I concatenate cells based on several if conditions?

    Hi people
    I wanted to post a question but am having difficulty navigating this forum so if I'm in the wrong place please excuse me.

    My question is this: I have a spreadsheet containing address lines 1,2,3,4 and want to turn it into one address block with "/n" being the divider for a new line. I know I could do a concatenate for the cells like for instance =concatenate(B2,"/n",C2,"/n",D2,"/n",E2) which would give me all the address lines with the /n divider... But how do I add an If function that only combines when the address line is not blank?

    So far I've tried variations of =if(E2<>"",concatenate(D2,/n",E2)... But basically I fall down when trying to do the conditions so i.e. address lines should only concatenate where there is data in the next proceeding column.

    I hope this makes sense to you guys. I know it's possible to be done. Please help

  2. #2
    Forum Expert RobertMika's Avatar
    Join Date
    06-22-2009
    Location
    Haverhill, UK
    MS-Off Ver
    Excel 2003-13
    Posts
    1,530

    Re: How do I concatenate cells based on several if conditions?

    With small help from VBA
    http://www.xl-central.com/concatenat...n-a-range.html
    If you are http://www.excelforum.com/image.php?type=sigpic&userid=125481&dateline=1392355029happy with the results, please add to the contributor's
    reputation by clicking the reputation icon (star icon).




    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.
    To undo, select Thread Tools-> Mark thread as Unsolved.
    http://www.excelaris.co.uk

  3. #3
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How do I concatenate cells based on several if conditions?

    Quote Originally Posted by RobertMika View Post
    Robert thanks for replying that's much appreciated. I don't have any previous experience of using VBA and So find it a bit daunting. Do you know a solution based on an excel formula? I was hoping to eventually be able to understand whatever it is I am writing.

    Thanks again

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do I concatenate cells based on several if conditions?

    Do you have circumstances where say line 1 is blank but line 2 is populated, or are they always populated in order? (ie if line 2 is populated line 1 will always be populated)
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How do I concatenate cells based on several if conditions?

    Quote Originally Posted by romperstomper View Post
    Do you have circumstances where say line 1 is blank but line 2 is populated, or are they always populated in order? (ie if line 2 is populated line 1 will always be populated)
    Hi Rory - yes if you think of it exactly as a postal address, where some addresses may be longer than others. I have 4 address line columns but some may only have two lines, some may have all four, but there will always be a first line of address. I need to get it so that if there is only one line it selects only that line of data without putting the "/n" in since there isnt a next line to follow. I know for certain you can do this using the IF condidiition but I havent been able to crack it.

    Would be forever appreciative if you could help!

  6. #6
    Forum Expert ben_hensel's Avatar
    Join Date
    01-26-2012
    Location
    Northeast USA
    MS-Off Ver
    Office 365
    Posts
    2,043

    Re: How do I concatenate cells based on several if conditions?

    Please Login or Register  to view this content.
    Like that?

  7. #7
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How do I concatenate cells based on several if conditions?

    Hi Ben, thank you that's almost right but its still adding the unwanted "/n" at the end which will basically create an unnecessary new line at the end. I only need the /n to identify where there is a next line of address following on. If it is the only line then I need it to end there.. (hope that makes sense). Many thanks for that I think that formula you sent is well on the way toward what I want. Unfortunately I havent been able to tweak it successfully to get precisely what I want

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do I concatenate cells based on several if conditions?

    If any of the lines can be blank (you answered my either/or with "Yes" ), you could try:
    =MID(IF(B2<>"","/n"&B2,"")&IF(C2<>"","/n"&C2,"")&IF(D2<>"","/n"&D2,"")&IF(E2<>"","/n"&E2,""),3,32767)
    I assume you don't want to end up with /n at the end?

  9. #9
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How do I concatenate cells based on several if conditions?

    Quote Originally Posted by romperstomper View Post
    If any of the lines can be blank (you answered my either/or with "Yes" ), you could try:
    =MID(IF(B2<>"","/n"&B2,"")&IF(C2<>"","/n"&C2,"")&IF(D2<>"","/n"&D2,"")&IF(E2<>"","/n"&E2,""),3,32767)
    I assume you don't want to end up with /n at the end?
    Thanks stomper this looks good. Im going to test it in a minute and let you know how it goes. I understand most of what is going on in that formula but could you tell me, what is the MID function (yes I am a real beginner) and also what is the final part of the string i.e. 3,32767

  10. #10
    Registered User
    Join Date
    07-10-2013
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: How do I concatenate cells based on several if conditions?

    Stomper you're a legend. It works... I'm likely to use this formula again so you've really helped save me alot of time and perplexment.
    Very many thanks for your help.
    Could you though, explain to me what the last part of the formula is saying?

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do I concatenate cells based on several if conditions?

    The formula prepends any cell text with /n (rather than appending) so if there's any address data the text will always start with "/n". The MID formula simply returns all the text from the 3rd character, thereby skipping the initial /n. (the 32767 is just the largest amount of text you can have in a cell).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Concatenate set cells based on duplicate value
    By timsilver in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-05-2013, 09:21 AM
  2. Concatenate cells with rows matching certain conditions ?
    By btc5 in forum Excel - New Users/Basics
    Replies: 6
    Last Post: 03-30-2013, 09:58 AM
  3. Concatenate cells based on a lookup value
    By Juliana33 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-24-2012, 02:11 AM
  4. concatenate based on conditions
    By tani001 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 04-29-2011, 08:41 AM
  5. Concatenate cells or ranges based on conditions
    By andrewdaus in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-01-2009, 06:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1