Remove whitespaces and empty lines in Excel using Regex
Remove whitespaces and empty lines in Excel using Regex
Wish to handle whitespaces in the most effective way? Use regular expressions to remove all spaces in a
cell, replace multiple spaces with a single character, trim spaces between numbers only, and more.
Whichever input data you are using, you'll hardly encounter a dataset without spaces. In most cases,
whitespace is good - you use it to visually separate di�erent pieces of information to make it easier to
perceive. In some situations, however, it may become evil - extra spaces can mess up your formulas
and make your worksheets almost unmanageable.
To understand the di�erence, let's see what is considered whitespace in each case:
1 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
The built-in TRIM function can only strip the space character that has value 32 in the 7-bit ASCII
system.
Regular expressions can identify a few di�erent forms of whitespace such as the space ( ), tab (\t),
carriage return (\r), and new line (\n). Additionally, there is the whitespace character (\s) that
matches all these types and comes extremely helpful for cleaning raw input data.
Knowing exactly what happens behind the scenes, it's a lot easier to work out a solution, right?
To add the function to your Excel, just copy its code from this page, paste it in the VBA editor, and save
your �le as a macro-enabled workbook (.xlsm).
The �rst three arguments are required, the last two are optional.
Where:
Replacement - the text to replace with. To remove whitespaces, you'd set this argument to either:
space character (" ") to replace multiple spaces with a single space character
Instance_num (optional) - the instance number. In most cases, you'll omit it to replace all instances
(default).
Match_case(optional) - a Boolean value indicating whether to match (TRUE) or ignore (FALSE) text
case. For whitespace, it is irrelevant and therefore omitted.
2 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
Pattern: \s+
Replacement: ""
To make it easier to manage your patterns, you can input the regex in a prede�ned cell and supply it
to the formula using an absolute reference like $A$2, so the cell address will remain unchanged when
copying the formula down the column.
3 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
Pattern: \s+
Please pay attention that this formula keeps one space character not only between words but also at
the beginning and end of a string, which is not good. To get rid of leading and trailing whitespace,
nest the above formula into another RegExpReplace function that strips spaces from the beginning
and end:
4 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
Leading whitespace:
Pattern: ^[\s]+
Trailing whitespace:
Pattern: [\s]+$
Pattern: ^[\s]+|[\s]+$
Replacement: ""
For example, to eliminate all spaces at the beginning and at the end of a string in A5, the formula is:
As shown in the screenshot below, this only removes leading and trailing whitespace. Spaces between
words remain intact creating a visually pleasing view for the reader's eye.
5 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
In the below dataset, suppose you wish to trim all leading/trailing spaces and all but one in-between
spaces, keeping multiple lines intact. To ful�l the task, you'll need two di�erent RegExpReplace
functions.
The �rst function replaces multiple spaces with a single space character.
The other one strips spaces from the beginning and end of a line:
6 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
Then, serve the above function to the text argument of another RegExpReplace that replaces one or
more consecutive whitespaces with the character you specify, e.g. a hyphen:
Pattern: \s+
Replacement: -
Assuming the source string is in A8, the formula takes this shape:
Or you can enter the patterns and replacements in separate cells like shown in the screenshot:
To match empty lines that do not have a single character from the start ^ of the current line up to the
next line \n, the regex is:
Pattern: ^\n
If your visually blank lines contain spaces or tabs, use this regular expression:
7 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
Just replace the regex with an empty string using this formula, and all blank lines will be gone at once!
Luckily, the RegEx Tools included with our Ultimate Suite are free of these limitations since they are
processed by Microsoft's .NET RegEx engine. This lets you construct more sophisticated patterns that
are not supported by VBA RegExp. Below you'll �nd an example of such regular expression.
8 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
To match a whitespace between any two digits, you can use the following look-arounds:
Pattern: (?<=\d)\s+(?=\d)
To create a formula based on the above regexes, here are two easy steps to perform:
1. On the Ablebits Data tab, in the Text group, click Regex Tools.
2. On the Regex Tools pane, select the source data, enter your regex, choose the Remove option, and
hit Remove.
To get the results as formulas, not values, remember to put a tick in the Insert as a formula check
box.
In a moment, you'll see the AblebitsRegexRemove function inserted in a new column to the right of the
original data.
Alternatively, you can input the regex in some cell, say A5, and insert the formula directly in a cell
using the Insert Function dialog box, where AblebitsRegexRemove is categorized under AblebitsUDFs.
9 of 10 2/17/2025, 8:28 AM
Firefox https://www.ablebits.com/office-addins-blog/remove-whitespaces-empty...
As this function is specially designed for removing strings, it requires only two arguments - the input
string and regex:
=AblebitsRegexRemove(A5, $A$2)
That's how to remove spaces in Excel using regular expressions. I thank you for reading and look
forward to seeing you on our blog next week!
10 of 10 2/17/2025, 8:28 AM