VBA & Macro Problems

Excel VBA: Remove Carriage Returns and Line Feeds from Text

Tauseeq MagsiTauseeq Magsi Nov 12, 2026 869 views

Question details

The user needs to remove carriage return and line feed characters from text extracted from a PDF using Excel VBA.

Excel VBA: Remove Carriage Returns and Line Feeds from Text
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
Text copied or extracted from a PDF contains unwanted line breaks, and the user wants to clean the value before using it elsewhere.
Observed behavior
TRIM removes extra spaces but does not remove the carriage return and line feed characters embedded in the text.
Before you start

Confirm that sourceValue contains the extracted text and that textValue is the variable you want to clean. In VBA, carriage return is Chr(13) and line feed is Chr(10), so both characters must be removed.

Solution 1Recommended

Remove carriage returns and line feeds with VBA Replace

Use this method when PDF-extracted text contains hidden line-break characters that TRIM leaves behind.

1
Open the VBA editor

In Excel, press Alt+F11, choose Insert > Module, and open a standard VBA module.

2
Trim the source value

Assign the source text to the target variable with: textValue = Trim(sourceValue)

3
Remove the carriage return

Add: textValue = Replace(textValue, Chr(13), "") to remove character 13 from the same text variable.

4
Remove the line feed

Add: textValue = Replace(textValue, Chr(10), "") to remove character 10 and finish cleaning the text.

5
Use the cleaned value

Pass textValue to the next operation, such as writing it to a cell: Range("A1").Value = textValue.

Working VBA pattern: textValue = Trim(sourceValue) textValue = Replace(textValue, Chr(13), "") textValue = Replace(textValue, Chr(10), "")

Clean PDF-Extracted Text with VBA in WPS Spreadsheets

Use WPS Spreadsheets to open compatible Excel workbooks, edit the VBA procedure when macro support is available, and apply Replace with Chr(13) and Chr(10) to remove hidden line breaks from extracted text.

  1. 1. Open the workbook in WPS: Launch WPS Office, select Spreadsheets, and open the XLSM or compatible Excel workbook containing the macro.
  2. 2. Open the macro editor: Use the Tools or Developer menu to open the VBA or macro editor, depending on your WPS version and macro support.
  3. 3. Add the cleanup code: In a standard module, assign Trim(sourceValue) to textValue, then use Replace(textValue, Chr(13), "") and Replace(textValue, Chr(10), "").
  4. 4. Run and verify the macro: Run the procedure from the macro editor and inspect the destination cell to confirm that the unwanted line breaks are gone.
Removes carriage returns and line feeds that TRIM cannot clearSupports familiar spreadsheet workflows for cleaning PDF-extracted textOpens and saves Microsoft Excel XLSX and XLSM workbooks for easier migrationProvides a lightweight interface for reviewing cleaned values and macro results
QA img-9

Frequently Asked Questions

Why does Excel TRIM not remove line breaks from PDF text?

TRIM removes extra spaces and compresses repeated spaces, but it does not remove carriage return character 13 or line feed character 10. Use Replace with Chr(13) and Chr(10).

What VBA characters represent a carriage return and a line feed?

Chr(13) represents a carriage return, while Chr(10) represents a line feed. PDF-extracted text may contain either or both characters.

Can I remove both characters in one line of VBA?

Yes. Use textValue = Replace(Replace(Trim(sourceValue), Chr(13), ""), Chr(10), "") to trim the value and remove both line-break characters.

Why does my Replace statement leave the original text unchanged?

Replace returns a cleaned string; it does not modify the variable automatically. Assign the result back to the same variable, such as textValue = Replace(textValue, Chr(13), "").

Will this code remove visible spaces as well as line breaks?

The Trim function removes leading and trailing spaces, while the two Replace calls remove carriage returns and line feeds. Internal spaces remain unchanged.