+ Reply to Thread
Results 1 to 18 of 18

Processing multiple lines of text in range of cells which contain CR and LF

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Processing multiple lines of text in range of cells which contain CR and LF

    Hi,

    I have a worksheet which is populated with approximately 10 cols of data with 36 rows of data.

    Each cell "can" have multiple lines of text. I need to process each cell on the sheet to do the following:
    '1-Replace LineFeed and Carriage returns with spaces
    '2-However, if a LineFeed and/or Carriage return appears on a "line" all by itself then i want to just delete LF and CR

    Example:
    Before processing: Cell A1 contains the following:
    ABCDEFG HIJKLM NOP.(cr)
    (cr)(lf)
    QRST UVWXY XAAAB(cr)(lf)


    i want the value in cell to be processed to look like this
    ABCDEFG HIJKLM NOP. QRST UVWXY XAAAB


    any idea how to do this.......i am enclosing a file showing an example of what i have and what i want to convert it to.......

    not sure how to do this.
    Attached Files Attached Files

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    Perhaps ...

    =TRIM(SUBSTITUTE(SUBSTITUTE(A1, CHAR(10), " "), CHAR(13), " "))

    or

    =CLEAN(A1)
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    no that wont work........i tried the replace thing.

    problem is if you look at this
    ABCDEFG HIJKLM NOP.(cr1)
    (cr2)(lf2)
    QRST UVWXY XAAAB(cr3)(lf3)

    (cr1) gets replaced with a space
    (cr2)(lf2) is removed and not replaced with anything just removed
    (cr3)(lf3) is replaced with two spaces and will be later removed with a trim.

    its the (cr2)(lf2) that i dont know how to deal with...........does this make sense what i am asking about?

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    Maybe:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    nope that does not work. it just does not handle the case i mentioned previously where (CR) and/or (LF) are on an empty line...in this case they should be removed and NOT replaced with spaces.

    Your code produces "two" spaces between "stuffects. Risk" and it should produce only one space.

    thanks for trying.

    this is a tough one ......not sure how to do it.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    no that wont work........i tried the replace thing.
    The SUBSITUTE formula works fine for me.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    I think the two spaces were a result of typing. Try this on your sample.

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    shg your right........let me try it on some other cells i have .......

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    shg, if i wanted to duplicate your formula in vba how would i do it?

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    shg,

    here is my code but it does not seem to work

    Please Login or Register  to view this content.
    i still keep getting two spaces not the one

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    i even tried this and it did not work........


    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    i even tried this

    Please Login or Register  to view this content.
    very strange.......because i think that should have worked.

    when i do the formula in the excel sheet

    =TRIM(SUBSTITUTE(SUBSTITUTE(A1, CHAR(10), " "), CHAR(13), " "))

    it works......only a single space but for some reason not when i use the vba code.......any ideas?

  13. #13
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    i am doing something wrong..........i think all of your suggestions should work just fine.........i am just doing something wrong........not sure what yet

    thanks for your help

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    Post the workbook with the code showing an example that doesn't work as you expect.

  15. #15
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    I had no problems with this VBA version of shg's formula:

    Please Login or Register  to view this content.
    Alf

  16. #16
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    Post the workbook with the code showing an example that doesn't work as you expect.
    ..................+1

  17. #17
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    hi all,

    thanks for helping. turns out all i needed to do was as someone posted.

    remove chr(10)'s and replace with single-space
    remove chr(13)'s and replace with single-space
    AND then remove all double-spaces and replace with a single space (had to create a loop until all doubles were gone)

    the above seem to fix my problem.......

    so thanks to all who helped.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Processing multiple lines of text in range of cells which contain CR and LF

    had to create a loop until all doubles were gone
    Or use WorksheetFunction.Trim.

+ 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. Reduce processing time for processing multiple excel sheets
    By rexer231 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2014, 09:25 AM
  2. Replies: 3
    Last Post: 12-12-2012, 04:01 PM
  3. Replies: 7
    Last Post: 11-27-2008, 02:58 PM
  4. [SOLVED] Autofit in cells with multiple lines of wrapped text.
    By John Keith in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2006, 03:10 PM
  5. RE: Autofit in cells with multiple lines of wrapped text.
    By John Keith in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-02-2006, 11:35 AM

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