VBA & Macro Problems

Use Office Script to Remove Carriage Returns in Excel Online

Huda QurayshiHuda Qurayshi Nov 13, 2026 869 views

Question details

The user needs to remove carriage-return characters from a range in Excel Online because the Replace dialog does not support Ctrl+J.

Use Office Script to Remove Carriage Returns in Excel Online
Product
Microsoft Excel Online
Device & OS
not provided
Scenario
A user is editing an Excel Online workbook and needs to clean line breaks from a selected data range.
Observed behavior
Excel Online does not support Ctrl+J in the Replace dialog, so carriage returns cannot be replaced directly; an Office Script can perform the replacement.
Before you start

Make sure the workbook is saved in OneDrive or SharePoint and opened in Excel Online. Confirm that the Automate tab is available and that the script range matches the cells you want to clean.

Solution 1Recommended

Remove carriage returns with an Office Script in Excel Online

Use this method when the Excel Online Replace dialog cannot find or replace carriage returns with Ctrl+J.

The script uses Range.replaceAll with "\r" to match carriage-return characters and replace them with an empty string. Adjust A1:A10 to the range that contains your data before running.

1
Open the workbook in Excel Online

Sign in to Microsoft 365 and open the target workbook in Excel Online from OneDrive, SharePoint, or the Office app.

2
Create a new Office Script

On the ribbon, select Automate > New Script. In the Code Editor, delete any existing placeholder code.

3
Paste and adjust the script

Paste the following code and change A1:A10 to the range you want to clean: function main(workbook: ExcelScript.Workbook) { const sheet = workbook.getActiveWorksheet(); const range = sheet.getRange("A1:A10"); range.replaceAll("\\r", "", { completeMatch: false, matchCase: false }); }

4
Run the script

Click Run in the Code Editor, then check the selected cells to confirm that carriage returns have been removed.

Remove carriage returns with an Office Script in Excel Online
Carriage returns removed: The script replaces carriage returns only in the range specified in the code. Update the range and run it again if your data expands.
Free Microsoft Office alternative

Use WPS Office to Clean Carriage Returns Without Office Scripts

If you need a simpler desktop workflow or do not have access to Office Scripts in Excel Online, WPS Office is a free, lightweight alternative. It opens .xlsx files with a familiar ribbon interface and lets you remove line breaks with standard find-and-replace controls, making it easy to switch from Microsoft Excel.

  1. 1. Download WPS Office: Download the free WPS Office installer from the official WPS website and complete the installation.
  2. 2. Open your Excel workbook: Launch WPS Spreadsheets, click Open, and select the .xlsx file that contains carriage returns.
  3. 3. Replace carriage returns: Press Ctrl+H to open Find and Replace, enter Ctrl+J in the Find what field, leave Replace with empty, and click Replace All.
Open and edit .xlsx workbooks in WPS Spreadsheets with full Microsoft Excel format compatibilityRemove carriage returns and line breaks with the familiar Find & Replace dialogFree, lightweight Office suite with a familiar ribbon UI and no Microsoft 365 subscription requirementSeamless migration from Excel Online while keeping workbook formatting and macros workflows
microsoft office alternative - wps office

Frequently Asked Questions

Why does Ctrl+J not work in the Excel Online Replace dialog?

Excel Online does not support the Ctrl+J control-character shortcut in the Replace dialog. Use an Office Script that calls Range.replaceAll for "\r", or open the workbook in desktop Excel or WPS Office to use Find & Replace.

Can I apply this Office Script to a different sheet?

Yes. The script uses getActiveWorksheet(), so select the target sheet before running it. To target a specific sheet, use workbook.getWorksheet("SheetName") instead.

Does this script remove all line breaks in the selected range?

The provided script replaces carriage returns (\r) only. If your cells contain line feed characters or CRLF pairs, change the search term to "\n" or "\r\n" in the code.

Do I need a Microsoft 365 license to use Office Scripts?

Office Scripts are available with supported Microsoft 365 work or school plans. If the Automate tab is unavailable, you can use WPS Office as a free alternative to clean the workbook.

Can WPS Office open and edit the same Excel Online file?

Yes. Download the workbook from OneDrive or SharePoint, open it in WPS Spreadsheets, and use Find & Replace to remove carriage returns. You can then save it back as a .xlsx file.