Dec
3

How to remove carriage returns (line breaks) from cells in Excel 2010, 2013, 2007

In this tip you'll find 3 ways to remove carriage returns from Excel cells. You'll also learn how to replace line breaks with other symbols. All solutions work for Excel 2013, 2010 - 2003

3 ways to remove carriage returns (line breaks) from your text in Excel

There can be different reasons for line breaks occurring in your text. Usually, carriage returns appear when you copy text from a webpage, get a workbook that already contains line breaks from a customer, or you add them yourself using Alt+Enter.

In any case, what you want to do now is delete carriage returns since they don't let you find a phrase and make column contents look disorganized when you turn on the wrap text option.

All these 3 ways are really quick. Feel free to pick the one that suites you best:

Note:Initially the terms "Carriage return" and "Line feed" were used in a typewriter and meant 2 different actions, you can find more at Wiki.

Computers and text processing software were created taking into consideration the typewriter specificities. That's why two different non-printable symbols are used now to indicate line break: "Carriage return" (CR, ASCII code 13) and "Line Feed" (LF, ASCII code 10). Windows uses 2 symbols one by one: CR+LF, and LF for *NIX systems.

Be careful: in Excel you can find both variants. If you import data from a .txt or .csv file, you are more likely to find Carriage Return + Line Feed. When you break a line using Alt+Enter , Excel inserts Line Feed only. In case you get .csv files from a person who uses Linux, Unix, etc., you'll find only Line Feeds again.

Remove Carriage Returns manually

Pros: the fastest way.

Cons: no any additional features :(.

Please find the steps for eliminating line breaks using Find and Replace:

1. Select all cells where you want to remove or replace carriage returns.
Select all cells where you want to remove or replace carriage returns.

2. Press Ctrl+H to open the Find & Replace dialog box.

3. In the Find What field enter Ctrl+J. It will look empty, but you will see a tiny dot.

4. In the Replace With field, enter any value to replace carriage returns. Usually, it is space to avoid 2 words join accidentally. If all you need is deleting the line breaks, leave the "Replace With" field empty.
Eliminating line breaks using Excel Find and Replace

5. Press the Replace All button and enjoy the result!

Delete line breaks using Excel formulas

Pros: you can use a formula chain / nested formulas for complex cell text processing. For example, it is possible to remove carriage returns and then eliminate excess leading and trailing spaces and those between words.

Or you may need to delete carriage returns to use your text as an argument of another function without changing the original cells. For example, if you want to be able to use the result as an argument of the function =lookup ().

Cons: you'll need to create a helper column and follow many extra steps.

1. Add the helper column to the end of your data. You can name it "1 line".

2. In the first cell of the helper column (C2), enter the formula to remove / replace line breaks. Here you can see several helpful formulas for different occasions:

  • Handle both Windows and UNIX carriage return/ line feeds combinations.
    =SUBSTITUTE(SUBSTITUTE(B2,CHAR(13),""),CHAR(10),"")
  • The next formula will help you replace line break with any other symbol (comma+space). In this case lines will not join and extra spaces will not appear.
    =TRIM(SUBSTITUTE(SUBSTITUTE(B2,CHAR(13),""),CHAR(10),", ")
  • If you want to remove all nonprintable characters from text, including line breaks:
    =CLEAN(B2)

Excel formula to delete carriage returns from cells

3. Copy the formula across the other cells in the column.

4. Optionally, you can replace the original column with the one where the line breaks were removed:

  • Select all cells in column C and press Ctrl + C to copy the data to clipboard.
  • Now pick the cell B2 and press the Shift + F10 shortcut. Then just press V.
  • Remove the helper column.

VBA macro to get rid of line breaks

Pros: Being created once, can be reused in any workbook.

Cons: you need to have the basic knowledge of VBA.

The VBA macro from the example below deletes carriage returns from all cells in the currently opened worksheet (active worksheet).

Sub RemoveCarriageReturns()
	Dim MyRange As Range
	Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    For Each MyRange In ActiveSheet.UsedRange
        If 0 < InStr(MyRange, Chr(10)) Then
            MyRange = Replace(MyRange, Chr(10), "")
        End If
    Next

	Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
End Sub

If you don't know VBA really well, see How to insert and run VBA code in Excel

Want to find more inventive tips, tricks and tutorials?

Please follow us on Google+:

35 Responses to "How to remove carriage returns (line breaks) from cells in Excel 2010, 2013, 2007"

  1. Luca says:

    Just a little mistake. You wrote: (LF, ASCII code 13) instead of (LF, ASCII code 10)

    Thank you for this useful article :-)

  2. Franco says:

    I used the macro and it was really good because I don't have the question mark in a box anymore :-) but now, the macro is deleting all the formulas I had in workbook and leaving only the answers :-(.

    • Alexander says:

      Hi Franco,

      The point is that my macro processed all the formulas on the sheet and not only those that contained a line break, my apologies. I've fixed the macro, please copy it anew from the article.

      If the result of the formula outputs a carriage return, I'm afraid nothing can be done about this. A macro cannot find where exactly in the formula the carriage return occurs, that is why it handles the formula's result and not the formula per se.

  3. daniel says:

    Thanks!!!

  4. Brian says:

    I'm trying to save an excel sheet to csv and I keep getting an extra line. I tried using your VBA method and couldn't get that to work (I'm assuming because I'm saving it to csv) and when I try the find and replace method it says no matches are found. Any other thoughts on why I keep getting an extra line?

    Thanks

  5. Rob says:

    Thank you SO much! This was driving me nuts, and formula #2 worked perfectly!

  6. Palani says:

    Fantastic! I was breaking my head for a long time before I landed in your page! Keep up the good work!

  7. Pravin S says:

    Nice.I am ver Happy!!!
    I am able to solve the issue with this
    Using
    ReplaceSubstring(CStr(nabGroupDoc.InternetAddress(0)),Chr(10),"")
    ReplaceSubstring(CStr(nabGroupDoc.InternetAddress(0)),Chr(13),"")

    Thanks a lot.

  8. Howard says:

    Hi Pravin
    I followed Solution 1, replacing 'carriage returns' with a 'comma'. I did this as I wanted to split addresses, originally in contained in a single cell, across columns using 'Text to columns', and thought the comma was a useful delimiter. The replace function appeared to work, however when I went on to use 'Text to columns' only the first piece of the text (before the comma) appeared in my original cell, no text in the columns to the right as I would usually expect. Can you help please? The data was sent to me in an Excel worksheet, but was originally extracted from an Access based database. Kindest regards, H

  9. Lmarr says:

    The same thing happened to me as it did to Pravin above: "I followed Solution 1, replacing 'carriage returns' with a 'comma'. I did this as I wanted to split addresses, originally in contained in a single cell, across columns using 'Text to columns', and thought the comma was a useful delimiter. The replace function appeared to work, however when I went on to use 'Text to columns' only the first piece of the text (before the comma) appeared in my original cell, no text in the columns to the right as I would usually expect. Can you help please? The data was sent to me in an Excel worksheet, but was originally extracted from an Access based database. Kindest regards, H"
    Could you please post if there is a solution. I find the only way around this is to Ctrl-x and Ctrl-v in the fx line. Very tedious when I have thousands to do.

  10. Mike says:

    Thanks for the great VBA macro it worked perfectly!

    Any ideas on how to remove/replace strike through text?

  11. Jason Friedman says:

    Thanks, the clean() information was helpful.

  12. Grace Lim says:

    Thanks. It helps.

  13. mrox says:

    So helpful! (Used the formula)
    Big thanks!!!

  14. Zeke says:

    Worked perfect, thanks.

  15. Priyanna says:

    Thank you, it worked perfectly! Saved me a lot of trouble.

  16. Hussein says:

    Thanks A lot

  17. Pratik says:

    Thanks a ton!!!! It's just marvelous :)

  18. JC says:

    Thank you for the macro! It really helped.

  19. Sam Cranwell says:

    Unfortunately I cannot get either the formula or the macro to work correctly on Excel for Mac 2011.

    I wish to use the formula to replace the line breaks with a comma and space. The formula supplied is missing a parenthesis off the end, but even so, with that added the formula only removes the line breaks. The extra comma and space are not being inserted.

    So I tried running the VBA but this had no effect at all.

    Is there likely to be any differences in using Excel for Mac?

    • Sam+Cranwell says:

      Ok, partly answering my own question here but:

      =SUBSTITUTE(B2,CHAR(13),", ")

      has worked for me. So I'm handling only Mac line breaks. The combination version of the formula seemed to omit the comma and space

  20. Pat says:

    The VBA code seemed to work at first, though a space seemed better than an empty string, but now I get a Run-time error '13': Type mismatch on the If 0 < InStr(MyRange, Chr(10)) Then statement.

  21. John says:

    Neat! thanks.

  22. Joe Blough says:

    These methods are great but do not seem to help with the problem I am having. Excel 2010 appends a hidden linefeed 0x0d followed by a hidden carriage return 0x0a at the end of each cell. I have a worksheet that concatenates the values in other cells to form a syntactically correct linux shell script, one shell command in each cell. Then when I output these cells to a text file so I can use them with linux, each line of my linux shell scripts has the 0x0d, 0x0a ending. The linux shell chokes on the 0x0d as I believe linux expects only a 0x0a to terminate each line of input.

    The methods here seem to remove unwanted lf and cr but not the ones excel puts at the end of every cell.

    Am I using these tools incorrectly?

    Thanks

    joe

    • Base says:

      @Joe
      Microsoft products place a CRLF to perform line breaks.
      You will need to further preprocess the file, I assume a CSV on your Linux machine to remove the Carriage returns.

      I used to have to do that when transferring files
      I suggest the *nix style command line command tr

      tr -d '\r' outfile

      Make sure you name your output file a different name to protect your document in case of error.

  23. santosh says:

    hi how to add new line character?

  24. santosh says:

    i am writing from Excel to text file. how to add new line character?

    • Base says:

      @Santosh

      adding a new line character shouldn't be hard if you have a constant character to key off of. In that case you can use substitute to replace key character with the key character and char(13)

  25. Neha says:

    CLEAN does not remove leading or trailing spaces. For this use =TRIM(CLEAN(B2))

Post a comment



Unfortunately, due to the volume of comments received we cannot guarantee that we will be able to give you a timely response. When posting a question, please be very clear and concise. We thank you for understanding!
Ultimate Suite for Excel Professionals
 
 
The best spent money on software I've ever spent!
Patrick Raugh
Ablebits is a fantastic product - easy to use and so efficient.
Debra Celmer
Excel is at its best now
Annie C.
I don't know how to thank you enough for your Excel add-ins
Jennifer Morningstar
Anybody who experiences it, is bound to love it!
Kumar Nepa
It's the best $100 we've ever spent!
Mike Cavanagh
I love the program, and I can't imagine using Excel without it!
Robert Madsen
One word… WOW!
Dave Brown
Love the products!
David Johnston
It is like having an expert at my shoulder helping me…
Linda Shakespeare
Your software really helps make my job easier
Jeannie C.
Thanks for a terrific product that is worth every single cent!
Dianne Young
I love your product
Brad Gibson
Awesome!!!
Sheila Blanchard