+ Reply to Thread
Results 1 to 14 of 14

Keep first word in each line, separate by semicolon and remove everything else

  1. #1
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Keep first word in each line, separate by semicolon and remove everything else

    Hello!


    I've come across a problem at work, where I have large spreadsheets with columns of cells like this:

    Apple ABC 123
    Pear de 4
    Banana 665 F

    That is, one relevant word followed by some other, irregular, stuff. Then a linebreak, a new relevant word, and other stuff. Note that the number of lines in each cell varies, from 0 to maybe ten.

    I need to copy-paste this into a Sharepoint sheet, and it needs to be on this form:

    Apple; Pear; Banana

    My first thought was that this would be a great thing to automate, and hence a reason to learn Excel macros, so I've bought Walkenbach's book "Excel 2013 Power Programming with VBA". However, I'm quite lost, to say the least.

    So my questions to you, is:
    a) It is at all possible to create a VBA macro that transforms a column of text in the way that I want it?
    b) How? What structure and functions should the code have?
    c) Can you recommend me any "key words" or information sources for this specific type of macros? I've realized that VBA is usable for way more things than I thought, so I don't even know where to read in Walkenbach's book... or elsewhere.


    Thank you!

    Alfred

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    12,322

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Maybe a non VBA solution.

    With text to column.

    After that just add the values in column A together.

    =a1&", "&a2&", "&a3
    Notice my main language is not English.

  3. #3
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Hi Oeldere,

    Unofortunately that doesn't work, since it doesn't remove the "junk". Also, I can't get text to columns to separate the text where the line-breaks are.

    Alfred

  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,705

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Hi adh2

    Welcome to the Forum!!!

    I'd take a stab at this were it not for this comment which could have several implications.
    Then a linebreak
    If you will, please attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    INCLUDE ANY CODE YOU'RE USING OR HAVE TRIED.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    If you want this try Split, Replace...
    Can you recommend me any "key words" or information sources for this specific type of macros
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 2000/3/7/10/13/16
    Posts
    48,373

    Re: Keep first word in each line, separate by semicolon and remove everything else

    If you have a string of text in a cell, with line-breaks, you would 1st need to break that apart. The line-break is char(10)

    upload a small (clean) sample workbook (not a pic) of what you are working with, and what your expected outcome would look like.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  6. #6
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Thank you, Jaslake and FDibbins for you replies.

    I have attached a workbook that illustrates what I want to acheive, with a "Before" and a "After" sheet.
    The fruit names are what I want to keep, the random letters, numbers and symbols should be discarded.

    I still don't see how to take apart the lines. It isn't possible to enter "char(10)" in text-to-columns?
    Also, the line-breaks are the only things that identifies the useful words from the useless ones. So the line-breaks would need to be removed and replaced by semicolons in one single step. And to make it even trickier, the number of words on each line, as well as the number of lines in each cell, is irregular.

    Alfred
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,629

    Re: Keep first word in each line, separate by semicolon and remove everything else

    here's my attempt using UDF

    i cant figure a way to split text up where there is nothing in between characters besides chr(10) though

    system wise..i cant figure it out...

    trimmed down version of the UDF

    Please Login or Register  to view this content.
    ps udf does not work for multiple cells in range...only 1 cell
    Attached Files Attached Files
    Last edited by humdingaling; 07-13-2015 at 03:30 AM. Reason: updated UDF
    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.

  8. #8
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,705

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Hi Alfred

    It's convoluted but it works on your sample data.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,629

    Re: Keep first word in each line, separate by semicolon and remove everything else

    thanks for jaslake code i was able to figure out how to complete the UDF

    Please Login or Register  to view this content.
    ps in testing
    if a single line item has a chr(10) at the end...it will give an error as the base assumption used for this code was number of chr(10) = number of splits
    Attached Files Attached Files
    Last edited by humdingaling; 07-13-2015 at 08:20 PM. Reason: remove debug line

  10. #10
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Thank you Humdingaling and Jaslake!

    I cant get it to work fully on my private computer (a mac), and I wasn't able to access Excelforum.com from work (apparently the site was blocked by the corporate internet filter ;P).

    But I've realized that Excel doesn't work identically on Mac and PC, so I have emailed the codes to my work email so will try them during the day!

    On a side-note, one of the differences I noticed yesterday is that on my PC at work I can replace the line-breaks with, for instance, three dashes, using the substitute function;
    =SUBSTITUTE(A1;CHAR(10);"---")
    On my Mac, this doesn't work; it will remove the line breaks but only replace them with a single space. I naively thought Excel would always be Excel, now I know better

  11. #11
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 2000/3/7/10/13/16
    Posts
    48,373

    Re: Keep first word in each line, separate by semicolon and remove everything else

    hmm try "-@-" instead?

  12. #12
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Re: Keep first word in each line, separate by semicolon and remove everything else

    Nope, still doesn't work. Neither do numbers, referencing another cell with text, or letters.

    Capture d’écran 2015-07-14 à 07.54.11.png

  13. #13
    Registered User
    Join Date
    07-12-2015
    Location
    Uppsala, Sweden
    MS-Off Ver
    2013
    Posts
    28

    Re: Keep first word in each line, separate by semicolon and remove everything else

    So, I tried your solutions today and they both worked perfectly!

    Thank you so much, you saved me heaps of time and helped me learn a lot of VBA!

  14. #14
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,705

    Re: Keep first word in each line, separate by semicolon and remove everything else

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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