+ Reply to Thread
Results 1 to 11 of 11

Function(s) to add or delete comma or space based on variable

  1. #1
    Registered User
    Join Date
    05-26-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Question Function(s) to add or delete comma or space based on variable

    Hello all,

    I'm trying to setup a system where commas will automatically be added (or subtracted) based on if a cell is empty or not. For Example:

    A1: Black
    A2: Metal
    A3: Shelf

    A5: Black, Metal, Shelf

    But if it reads:

    A1: Black
    A2: Metal
    A3:

    A5: Black, Metal

    or alternatively:

    A1: Black
    A2:
    A3: Shelf

    A5: Black, Shelf

    How do I go about doing this? Trial and error hasn't provided me with a result

    Thanks!
    Last edited by imagewis; 05-07-2015 at 04:47 PM.

  2. #2
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Function(s) to add or delete comma or space based on variable

    In A5

    =SUBSTITUTE(TRIM(CONCATENATE(A1&" "&A2&" "&A3))," ",", ")

    Row\Col
    A
    1
    Black
    2
    Metal
    3
    Shelf
    4
    5
    Black, Metal, Shelf



    and

    Row\Col
    A
    1
    Black
    2
    3
    Shelf
    4
    5
    Black, Shelf
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  3. #3
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Function(s) to add or delete comma or space based on variable

    Or simply,
    =SUBSTITUTE(TRIM(A1&" "&A2&" "&A3)," ",", ")

    taking into account the there may be multiple words in a single cell, this wold work as well
    EDIT: will change second formula
    Last edited by berlan; 05-07-2015 at 05:13 PM.

  4. #4
    Registered User
    Join Date
    05-26-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Function(s) to add or delete comma or space based on variable

    Quote Originally Posted by berlan View Post
    Or simply,
    =SUBSTITUTE(TRIM(A1&" "&A2&" "&A3)," ",", ")

    taking into account the there may be multiple words in a single cell, this wold work as well
    EDIT: will change second formula
    Thank you, I look forward to seeing the seconds formula as some cells will contain more than one word.

  5. #5
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Function(s) to add or delete comma or space based on variable

    Thank you for the feedback

    BTW, you don't need another formula to accommodate cells with more than one word. The formula provided will do it just fine

    Example:

    Row\Col
    A
    1
    Black
    2
    Metal Silver Gold
    3
    Shelf
    4
    5
    Black, Metal, Silver, Gold, Shelf

  6. #6
    Registered User
    Join Date
    05-26-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Function(s) to add or delete comma or space based on variable

    Quote Originally Posted by AlKey View Post
    Thank you for the feedback

    BTW, you don't need another formula to accommodate cells with more than one word. The formula provided will do it just fine

    Example:

    Row\Col
    A
    1
    Black
    2
    Metal Silver Gold
    3
    Shelf
    4
    5
    Black, Metal, Silver, Gold, Shelf
    Actually, I would like it be able to do something like this:

    A1: Black
    A2: Metal
    A3: Shelf
    A4: Large Size
    A5: In stock in NYC

    A7: Black, Metal, Shelf, Large Size, In stock in NYC

    or:

    A1: Black
    A2: Metal
    A3: Shelf
    A4:
    A5: In stock in NYC

    A7: Black, Metal, Shelf, In stock in NYC

    etc...

  7. #7
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Function(s) to add or delete comma or space based on variable

    The best way do deal with concatenating values would be to use VBA. Here is what you need to do:

    1. Copy code below
    2. Press Alt and F11 on your keyboard to open VB Editor
    3. Click on Insert and select Module
    4. Paste code into Module and close VB Editor.

    Don't forget to save your worbook as Macro-Enabled workbook.

    Please Login or Register  to view this content.
    then use custom formula

    =Concat_Range(A1:A5,", ")

    Row\Col
    A
    1
    Black
    2
    Metal*
    3
    Shelf*
    4
    Large Size
    5
    In stock in NYC
    6
    7
    Black, Metal*, Shelf*, Large Size, In stock in NYC

  8. #8
    Registered User
    Join Date
    05-26-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Function(s) to add or delete comma or space based on variable

    Quote Originally Posted by AlKey View Post
    The best way do deal with concatenating values would be to use VBA. Here is what you need to do:

    1. Copy code below
    2. Press Alt and F11 on your keyboard to open VB Editor
    3. Click on Insert and select Module
    4. Paste code into Module and close VB Editor.

    Don't forget to save your worbook as Macro-Enabled workbook.

    Please Login or Register  to view this content.
    then use custom formula

    =Concat_Range(A1:A5,", ")

    Row\Col
    A
    1
    Black
    2
    Metal*
    3
    Shelf*
    4
    Large Size
    5
    In stock in NYC
    6
    7
    Black, Metal*, Shelf*, Large Size, In stock in NYC
    Any way to do what I want without using macros?

  9. #9
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Function(s) to add or delete comma or space based on variable

    A quick but rather manual way,
    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor mahju's Avatar
    Join Date
    11-27-2010
    Location
    Pakistan, Faisalabad
    MS-Off Ver
    Excel 2010 plus
    Posts
    730

    Re: Function(s) to add or delete comma or space based on variable

    Use "&" with if condition as in attached
    In Column formula in the last cell is different
    thanks
    Attached Files Attached Files
    Mark the thread as solved if you are satisfied with the answer.


    In your first post under the thread tools.

    Mahju

  11. #11
    Registered User
    Join Date
    05-26-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Function(s) to add or delete comma or space based on variable

    Quote Originally Posted by berlan View Post
    A quick but rather manual way,
    Please Login or Register  to view this content.
    Awesome, that's exactly what I was looking for

    One more quick question:

    What if I want text to be automatically added if the cell is populated? Ex:

    A1: 24
    A2: shelf
    A3: black

    A5: 24 part, shelf

    alternatively:

    A1:
    A2: shelf
    A3: black

    A5: shelf, black

    Thanks again!
    Last edited by imagewis; 05-08-2015 at 11:50 AM.

+ 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. Comma delimited numbers with space after comma
    By Cicada in forum Excel General
    Replies: 5
    Last Post: 04-23-2015, 09:32 AM
  2. [SOLVED] Convert space between first and last name to comma
    By Vaslo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-04-2014, 03:06 PM
  3. [SOLVED] Replace space to comma
    By rarementality in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-13-2014, 09:15 AM
  4. Replies: 4
    Last Post: 07-25-2013, 05:28 AM
  5. Removing a space after a comma
    By DebbieK9 in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 04-01-2005, 06:06 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