+ Reply to Thread
Results 1 to 27 of 27

Adding |Pipe| to values in cells

  1. #1
    Registered User
    Join Date
    09-16-2009
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    8

    Adding |Pipe| to values in cells

    Hi I have a column of barcodes. I need to get a formula or script to put |Pipes| as the before the 1st and after the last character in the cell.

    E.g. barcode 123456789 should be converted to |123456789|

    I then want all the individual barcodes to appear in 1 cell next to each other

    E.g. in one cell |123456789|987654321|244346473|686541234|479854635|132498569| and so on.

    Thanks in advance!
    Last edited by talan; 09-21-2009 at 01:13 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Hi talan, welcome to the forum.

    This is called concatenation and is simple with just a few cells. STRING CONCATENATION is not really a built-in function of Excel (stringing together an entire range) so has to be added as a UserDefinedFunction (UDF).

    Your needs are pretty simple. Here is a very basic UDF that will string together any range you list:
    Please Login or Register  to view this content.
    How to install the User Defined Function:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The function is installed and ready to use.
    ========

    In a nutshell, the function is used like so:

    =CONCATRANGE(A1:A100,"|")


    The first parameter is the range
    The second parameter is the delimiting character you want.

    Since you want the PIPE to appear before and after your formula, insert those manually, like so:

    ="|" & CONCATRANGE(A1:A100, "|") & "|"
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Actually, I realized we could just add the "capping" of delimiting characters to each end as an optional parameter in the UDF, since we're using a UDF anyway.

    Here's the updated code:
    Please Login or Register  to view this content.
    And the updated formula use would be:

    =CONCATRANGE(A1:A100, "|", 1)

    The added third parameter is optional. If you leave it off or use a zero, then the UDF will not put capping delimiters at the start and end. If you put in a 1, you will see them.


    NOTE: Regardless of which UDF you use, if you concat a LONG column of values together, you may not be able to see the whole thing in Excel's display. That doesn't mean they aren't there.

    You can copy the cell with the formula, then EDIT > PASTE SPECIAL > VALUES to turn it into a flat string.
    Last edited by JBeaucaire; 09-16-2009 at 08:23 PM.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Adding |Pipe| to values in cells

    Another variant based on column based data which avoids need for iteration would be:

    Please Login or Register  to view this content.
    the above utilise's JB's optional prefix/suffix flag and is called in the same way

    Please Login or Register  to view this content.
    the premise here is to post the values to a variant array and then re-join the values based on the delimiter of choice.... the above is definitely less robust than JB's however in terms of handling blanks should they be interspersed amongst valid values.

  5. #5
    Registered User
    Join Date
    09-16-2009
    Location
    Auckland, New Zealand
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    JBeaucaire: You are a LEGEND! Works great. Thank you so much!

    [SOLVED]

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Quote Originally Posted by DonkeyOte View Post
    Please Login or Register  to view this content.
    ...the above is definitely less robust than JB's however in terms of handling blanks should they be interspersed amongst valid values.
    Actually, that's not true. Yours "almost" handled that, too. One small tweak and I think we've got a much better basic string concatenation tool that uses no loop at all. Awesome idea, Don.

    Here's the adjusted version:
    Please Login or Register  to view this content.
    Still utilized the same way:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 09-21-2009 at 01:47 AM.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Hmm, I think I just discovered what you meant by "column based" data. This array approach would need some more massaging to be universally usable for

    1) columnar data
    2) row data
    3) 2 dimensional data

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Adding |Pipe| to values in cells

    JB, the issue with that approach remains the same in that blanks in the referenced range will cause issues... if one were to assume no blanks (interspersed or not) then there's no need for the Replace at all.

    I think this approach would work correctly regardless of position of blanks in range:

    Please Login or Register  to view this content.
    I used WF Trim rather than VBA Trim, the latter would not return correct result

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    By hardcoding the " " into the replace, it turns one cell two=word strings ("Apple cart") into two cell 1=word strings (Apple|cart).

    My version above solves that, but it doesn't do row ranges. How would we go with A1:I1 instead of A1:A9?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Adding |Pipe| to values in cells

    Correct. Let's assume

    A1: Apple Cart
    A2: Bananas
    A3: [blank]
    A4: Carrots

    and

    A5:A100: [blank]
    Neither version will work, output of my prior code would be:

    |apple|cart|bananas|carrots|
    And yours:

    |apple cart|bananas|carrots|||||||||||||||||||||||||||||||||||||||||||||||||
    One workaround would be to use Evaluate to replace the bona fide spaces prior to processing the strings and then revert to space thereafter, eg:

    Please Login or Register  to view this content.
    which would generate:

    |apple cart|bananas|carrots|
    though of course if there are valid ^ in the string...

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Quote Originally Posted by DonkeyOte View Post
    ...And yours:
    Please Login or Register  to view this content.
    ?? My final solution to the OP was:
    Please Login or Register  to view this content.
    And from your sample data suggested above, that UDF gives the results:
    Please Login or Register  to view this content.
    I'll have to analyze your suggestion, but I still think your original version works with the amendment I suggested. The only thing I'm trying to learn is how to fill the vData with values when the RngVals is in row format.

  12. #12
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Adding |Pipe| to values in cells

    Sorry by your suggestion I was referring to your initial revision to my approach!

    The latest version should work for blanks (wherever they appear) and cater for strings that contain spaces without need for iteration.

    I will put together a version for vector/matrix.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    My first tweak, then a "possible" loop added that will only loop very briefly if at all. For StrConcatenation, one would think the blanks would be minimal, so this is most likely the smallest additional calcs, if any, and it stays with the original approach to do the other 99% of the work.

    Please Login or Register  to view this content.

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Adding |Pipe| to values in cells

    I have little doubt this could be streamlined but I need to get on with some other bits & pieces so in the meantime...

    Please Login or Register  to view this content.
    this should work for both vectors & matrices regardless of blanks etc... for everything except strings that contain valid ^

    regards ^: it would pretty straightforward to create a constant array of "unusual chars" and iterate the array until such time as you find a char not present in the range of values being processed and subsequently code that into the replace function.

  15. #15
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    Hi all,

    I have the exakt same problem and I think I have tried every code string available here.

    It will not work. However, I'm confident that the error is on my side.

    Problem:
    Need to concatenate with a Pipe | and as per thread topic, same problem.

    One suggested solution WITH an output that would solve my issue does not work in my Excel.

    Do we have a "final" working code for this?

    Wanted solution.

    I have a range:
    42100
    57734
    50557
    77309
    48158
    28506
    34994
    223904
    269379
    21535
    290704
    201233

    I would like to have one cell where the result should be like this:
    42100|57734|50557|77309|48158|28506|34994|223904|269379|21535|290704|201233


    According to JBeaucaire, he made it happen.

    Question:
    Which code should I use?

    /Martin

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    The original suggestion in post #2 does the job. Install that into a sample workbook and try it with your sample data. IF you can't make it work, post your workbook so I can see your work/attempt.

  17. #17
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    Thank you,

    1. Copied the code from post #2
    Function ConCatRange(Rng As Range, Delim As String) As String
    'Usage: =CONCATRANGE(A1:A20, ",")
    Dim Cell As Range
    Dim sbuf As String

    For Each Cell In Rng
    If Len(Cell.Text) > 0 Then sbuf = sbuf & Cell.Text & Delim
    Next

    ConCatRange = Left(sbuf, Len(sbuf) - Len(Delim))
    End Function

    2. Opened and pasted the code:
    https://www.dropbox.com/s/zqn6k19l8n...-%201.jpg?dl=0

    3. Saved, Alt+Q, Saved and restarted workbook.

    4. Opened and tried the operation:
    https://www.dropbox.com/s/180t39llhm...-%202.jpg?dl=0

    Usage (with and without space after comma): =ConCatRange(E218:E229, " | ") and =ConCatRange(E218:E229," | ")

    TEST ENDED

    Here is the workbook:

    https://www.dropbox.com/s/5686a4hrip...ANGE.xlsm?dl=0

    Cheers!

    /Martin

  18. #18
    Banned User!
    Join Date
    10-29-2012
    Location
    Europe
    MS-Off Ver
    2013, 2016
    Posts
    318

    Re: Adding |Pipe| to values in cells

    @Martin

    You have no code in your file.
    You have to copy this code in module1:

    Please Login or Register  to view this content.
    and then in E26 write this: =CONCATRANGE(D11:D22, ",").
    With red font is the delimitator (you can change with what you want).

  19. #19
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    Indi_Ra,

    Sorry to all readers.

    I made an error when I saved the file yesterday. I made so many tests before I posted that I linked to the wrong book.

    And my previous post is still valid. It will not work.

    Here is the correct one:
    https://www.dropbox.com/s/5686a4hrip...ANGE.xlsm?dl=0


    /Martin

  20. #20
    Banned User!
    Join Date
    10-29-2012
    Location
    Europe
    MS-Off Ver
    2013, 2016
    Posts
    318

    Re: Adding |Pipe| to values in cells

    Use this formula:

    =CONCATRANGE(D11:D22; "|")

    Copy this formula (with the equal sign) in cell where you want concatenations.

  21. #21
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    Hello Indi_Ra

    Thank you for your swift reply.

    I know that the equal sign must be used before doing an operationg like this. However, I don't know how to save the "=" to show what I did. Of course I could have started with a space. I will do this now.


    Here is what happens if I use this operator:
    https://www.dropbox.com/s/kf8vyncx6s...0USED.jpg?dl=0

    Here is how I use it =ConCatRange(D7:D18, "| "). Please remember that I have added a space, because otherwise you might think I did not use the "=" before the operator.

    I used this to acheive what does not work for me.

    Indi_Ra, did you try my sheet? Did it work when my workbook was used?

    https://www.dropbox.com/s/5686a4hrip...ANGE.xlsm?dl=0

    /Martin

  22. #22
    Banned User!
    Join Date
    10-29-2012
    Location
    Europe
    MS-Off Ver
    2013, 2016
    Posts
    318

    Re: Adding |Pipe| to values in cells

    Pay attention

    =CONCATRANGE(D11:D22; "|")

    in this formula is ";" not ",". maybe your regional settings...

  23. #23
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    Indi_Ra,

    Thank you for being persistent.

    I paid attention to what code to use, but my mind was stuck to use the exactness of the code. So I used , as specified, not giving any thought at all to ; as a regional option.


    It works exactly as I want it to work.

    Thank you all!

    /Martin

  24. #24
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adding |Pipe| to values in cells

    Glad you got it worked out.

    As it appears you've reached a conclusion, I've marked this thread SOLVED for you.
    FYI, this is done through the Thread Tools located above the first post in this thread. Thanks.

  25. #25
    Registered User
    Join Date
    11-27-2014
    Location
    Sweden
    MS-Off Ver
    2003
    Posts
    8

    Re: Adding |Pipe| to values in cells

    JBeaucaire,

    Thank you.

    It seems to be 2 solutions:
    Variant 1 and 2.

    The difference is the usage of the separator. Usage is , or ;

    =CONCATRANGE(D11:D22; "|")

    =CONCATRANGE(D11:D22, "|")

    ------------------------------------------------------

    Function ConCatRange(Rng As Range, Delim As String) As String
    'Usage: =CONCATRANGE(A1:A20, ",")
    Dim Cell As Range
    Dim sbuf As String

    For Each Cell In Rng
    If Len(Cell.Text) > 0 Then sbuf = sbuf & Cell.Text & Delim
    Next

    ConCatRange = Left(sbuf, Len(sbuf) - Len(Delim))
    End Function

    ------------------------------------------------------


    /Martin

  26. #26
    Registered User
    Join Date
    06-25-2015
    Location
    Panama
    MS-Off Ver
    Office 2010
    Posts
    2

    Re: Adding |Pipe| to values in cells

    Thanks so much for your help, you have saved my day!

  27. #27
    Registered User
    Join Date
    06-25-2015
    Location
    Panama
    MS-Off Ver
    Office 2010
    Posts
    2

    Talking Re: Adding |Pipe| to values in cells

    Thanks so much for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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