Remove Whitespaces at the Beginning of a Newline in Google Sheets

We can use a Regex formula or the Data > Data clean-up > Trim whitespace to remove whitespace at the beginning of a newline in Google Sheets.

The TRIM function won’t return the desired result when you use it for the said purpose.

The purpose of TRIM is to remove spaces (ASCII 32) from a text in a cell. It can be repeated spaces, leading or trailing spaces.

There is no way to specify a Newline in it.

TRIM Spaces and Issue with Newline Character

Note:- Please note that whitespace is any character that represents (or renders as) a space.

For example, I have the below texts in A1:C1.

A1 – Cat
B1 – and
C1 – Dog

=A1&char(9)&B1&char(9)&C1

The above formula will return the string “Cat and Dog” similar to the below one.

=A1&char(32)&B1&char(32)&C1

Both char(9) and char(32) render a space.

You May Like: Learn Google Sheets CHAR Function and How to Use It in QUERY Formula.

Let’s back to our topic.

If we use a formula to remove whitespaces at the beginning of a Newline, we may require to match a pattern, i.e., a line break and whitespace(s).

What we will do here in the formula is match the required pattern [a line break and whitespace(s)] in a search operation using an RE2 regular expression and substitute it with just a line break.

Examples to Removing Whitespaces at the Beginning of a Newline in Google Sheets

As I’ve mentioned at the beginning, there are two methods. Let’s go to them one by one.

I am starting with the menu command.

Using Data Cleanup

Steps:

  1. Select A2:A11.
  2. Go to the Data menu and select Data Clean-up > Trim whitespace. That’s it!

The above is the quickest way to remove whitespaces at the beginning of every Newline in Google Sheets.

Points to be Noted:-

The above action will also remove all the leading, trailing, and repeated whitespace(s).

There is one more very important point.

Please check the values in cells A10 and A11. Visually, both are the same.

But in cell A11, I have copied the values from the range A2:A8 and pasted them in it, whereas, in cell A10, I have entered them manually.

As a result, in cell A11, the Newline is a carriage return (ASCII 13), whereas, in cell A10, it is the Newline character (ASCII 10).

You can test it using =code(mid(A10,14,1)) and =code(mid(A11,14,1)) formulas.

So, when you apply the above menu command, cell A11 may lose the formatting.

In that case, once you have applied the data cleanup, go to that cell (here cell A11) and hit the enter key.

Whitespaces at the Beginning of a Newline - Data Cleanup in Google Sheets

Formula to Remove Whitespaces at the Beginning of a Newline Character

Some of you may prefer a formula-based approach.

If you are one among them, try the below array formula in cell B2. It will expand down.

=ArrayFormula(REGEXREPLACE(trim(A2:A11),"\n\s+",char(10)))

What is the role of the TRIM here?

It removes the extra spaces.

What is the role of the REGEXREPLACE in the formula above?

Here you go!

\n – It matches a newline character.

\s – To match any whitespace character.

+ – It matches the previous token (here, whitespace) between one and unlimited times.

You can remove the ArrayFormula function if the reference is not an array, for example, A2 instead of A2:A11.

That’s all about how to remove whitespaces at the beginning of a Newline in Google Sheets.

Thanks for the stay. Enjoy!

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

How to Perform Case-Sensitive XLOOKUP in Excel with EXACT

We may not always need to perform a case-sensitive XLOOKUP for product names in...

Filter the Last 7 Days in Excel Using the FILTER Function

You may have records spanning several days and want to filter the last 7...

Find Missing Dates in Excel

You can find missing dates in a list in Excel using either a VLOOKUP...

Generate Unique Random Numbers in Excel with Dynamic Arrays

Unique random numbers refer to random numbers without duplicates. Generating such numbers is straightforward...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.