+ Reply to Thread
Results 1 to 9 of 9

Concatenating columns ignoring blanks

  1. #1
    Registered User
    Join Date
    05-04-2012
    Location
    uk
    MS-Off Ver
    2013
    Posts
    7

    Concatenating columns ignoring blanks

    Hi all,

    i have a number of columns of data that I wish to concatenate together, separated by commas for pasting into another application. Easy enough to do using the concatenate function, but since a lot of cells are blank, I am getting output form the concatenate function with long strings of commas.

    My concatenate function
    =CONCATENATE(AP2,", ",AQ2,", ",AR2,", ",AS2,", ",AT2,", ",AU2,", ",AV2,", ",AW2,", ",AX2,", ",AY2,", ",AZ2,", ",BA2,", ",BB2,", ",BC2,", ",BD2,", ",BE2,", ",BF2,", ",BG2,", ",BH2,", ",BI2,", ",BJ2,", ",BK2,", ",BL2,", ",BM2,", ",BN2)

    Example output
    , , , , , , , , , , , , , , , , , , , , , , , XXX,
    , , , , , , , , , , , , , , , , , , , , , , , XXX, YYY



    What is the easiest way to get the output I need, which in the first example above would be XXX, and in the second example would be XXX, YYY

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,723

    Re: Concatenating columns ignoring blanks

    You could change your formula to this:

    =SUBSTITUTE(SUBSTITUTE(CONCATENATE(AP2,", ",AQ2,", ",AR2,", ",AS2,", ",AT2,", ",AU2,", ",AV2,", ",AW2,", ",AX2,", ",AY2,", ",AZ2,", ",BA2,", ",BB2,", ",BC2,", ",BD2,", ",BE2,", ",BF2,", ",BG2,", ",BH2,", ",BI2,", ",BJ2,", ",BK2,", ",BL2,", ",BM2,", ",BN2),", ,",","),",,","")

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    05-04-2012
    Location
    uk
    MS-Off Ver
    2013
    Posts
    7

    Re: Concatenating columns ignoring blanks

    Hi Pete, that is closer but not exactly what I need.

    Previous output
    , , , , , , , , WWW, , , , , , , , , , , , XXX, YYY, ZZZ, ,

    Output using your formula:-
    , , , , WWW, , , , , , XXX, YYY, ZZZ,

    PS I have up to 24 consecutive commas in some cells. I do not mind having helper columns as this is purely a calculation sheet for an output that I paste elsewhere.

  4. #4
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,723

    Re: Concatenating columns ignoring blanks

    Okay, you can do it like this:

    =SUBSTITUTE(SUBSTITUTE(TRIM(IF(AP2="","",AP2&", ")&IF(AQ2="","",AQ2&", ")&IF(AR2="","",AR2&", ")&IF(AS2="","",AS2&", ")&IF(AT2="","",AT2&", ")&IF(AU2="","",AU2&", ")&IF(AV2="","",AV2&", ")&BN2&"/"),", /",""),"/","")

    You can see in the section coloured blue that the comma-space only gets added in if the cell is not empty. You will need to complete this for the cells from AV2 up to the BN2 term (coloured red). Notice that I am adding a slash character at the end, so that I can then remove the trailing ", /" if BN2 is empty or the final slash if it is not empty.

    Hope this helps.

    Pete

  5. #5
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Concatenating columns ignoring blanks

    Are there any spaces pre existing in any of the cells?

    If not, you could do

    =SUBSTITUTE(TRIM(CONCATENATE(AP2," ",AQ2," ",AR2," ",AS2,etc))," ",", ")

  6. #6
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Concatenating columns ignoring blanks

    If there are pre existing spaces in the cells...

    =SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(CONCATENATE(AP2,"|",AQ2,"|",AR2,"|",AS2,etc)," ","^"),"|"," "))," ",", "),"^"," ")

  7. #7
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Concatenating columns ignoring blanks

    Are you able/open to using a VBA function for this?

    If so, the code is at this post:

    https://www.excelforum.com/showthread.php?p=3096647

    Then, after installing the code, to use it enter this array formula**:

    =concatall(IF(AP2:BN2<>"",AP2:BN2,""),", ")

    ** array formulas need to be entered using the key
    combination of CTRL,SHIFT,ENTER (not just ENTER).
    Hold down both the CTRL key and the SHIFT key
    then hit ENTER.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  8. #8
    Registered User
    Join Date
    05-04-2012
    Location
    uk
    MS-Off Ver
    2013
    Posts
    7

    Re: Concatenating columns ignoring blanks

    Hi all, many thanks for there response. In the end I used a number of helper columns as I was in a rush to supply the output.

    My solution that I used (nowhere near as elegant as the above..)
    The columns containing data I wished to concatenate were in AR to BP, some cells containing blanks. Row 1 contained headers, rows 2 and below contained the data.

    The first concatenating column in BQ
    =IF(AR2="","",AR2)

    The second concatenating column in BQ, dragged across to CO
    =IF(AS2="",BQ2,CONCATENATE(BQ2,",",AS2))

    This second concatenation column ensured that i didn't add a blank column, avoiding the need to edit the output which previously contained a string of commas.
    However, the output could begin with a comma if BQ was blank, so I used the following function to trim the last concatenation column (CO)
    =IF(LEFT(CO2,1)=",",RIGHT(CO2,LEN(CO2)-1), CO2)

    As I said, not elegant...

    I would be interested to see the most elegant solution to concatenate a number of columns, containing a mix of data and empty cells, to a comma separated output.

    Many thanks all. I will keep this bookmarked for when I come across a similar problem.
    Last edited by rapscalli; 02-10-2015 at 11:04 AM.

  9. #9
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Concatenating columns ignoring blanks

    Quote Originally Posted by rapscalli View Post

    I would be interested to see the most elegant solution to concatenate a number of columns, containing a mix of data and empty cells, to a comma separated output.
    Here's a small sample file created in Excel 2010 that demonstrates the VBA function. The data range is B1:F1.
    Attached Files Attached Files

+ 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] Data Cleanup - ignoring blanks, ignoring rows with string values etc.
    By Huyaku in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-21-2015, 08:17 AM
  2. Combine Dates from 10 Columns While Ignoring Blanks
    By DKGODFREY in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-16-2014, 11:50 AM
  3. Find missing data in columns ignoring blanks, separated by semicolon
    By Cboggie in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-28-2014, 10:04 PM
  4. [SOLVED] Compare two ranges of data and give unique values in two different columns ignoring blanks
    By rampulaparthi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2013, 04:17 PM
  5. concatenating three columns without blanks
    By nujwaan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2009, 11:25 AM

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