In this article, we will learn How to remove Unnecessary Empty Characters in Excel.
What are leading and trailing spaces
In Excel, Data extraction and cleaning is the first and most crucial task of any data analysis project. In my five years of experience, I have received data that looked unstructured, but using excel TRIM or CLEAN functions I have turned them into meaningful reports. Sometimes text values contain leading, trailing, or multiple embedded spaces and the process of cleaning these empty characters or spaces is a part of data cleaning. Data cleaning in excel is easy. Now Let's learn how to clean text values in cells in Excel.
Remove all Empty Characters at once in Excel
There are steps involved in the method. First is the selection part, type formula and formula completion.
First : Select the required cells where you require the cleaned data or output cells
Second : Type formula
TRIM function is a TEXT function in Excel used to remove all extra spaces from the text string except for single space between words.
text : text value in quotes or cell reference
Third : At the completion of each formula we use Enter key to see the output of the formula used. But instead of using just Enter, use Ctrl + Enter key together
All of these might be confusing to understand. Let's understand how to use the function using an example. Here We need to remove them so that the formula referencing these cells calculate correctly. Column A contains text with leading spaces in front of the text. These spaces have been removed by using the Trim Function. See below screenshot:
There are two ways we can remove these spaces:
TRIM formula to remove empty characters
Trim function removes all spaces from a text string except for single spaces between words.
First select the cells where you require the clean data to be extracted or output cells. So we selected range C1:C5 and type the formula given below
Use the formula:
Now to complete the process we just need to use Ctrl + Enter in place of using just Enter.
If the data contains leading spaces & more than one space in between the text then the Trim Function will not remove all of them. Look at Cell A3 contains text with spaces in the middle of the text. Trim will remove all the leading space except a single space in between 2 words. See below screenshot:
Alternatively, we can use the Substitute Function in case we know our text contains spaces in between the words and need to be removed.
See below picture. The Substitute function replaces the space in between the text with “nothing”. Hence, the space gets removed.
If the data contains a leading space & more than one space in between the text then the Substitute Function will remove all of them. Cell A1 contains text with leading spaces or trailing spaces in between the text. The Substitute function will remove all the spaces.See the below screenshot:
Use the formula :
The above formula removes all occurrences of spaces unlike TRIM function which leaves single spaces in between words.
As you can see in Column C there are no extra spaces.
One more way :
We can also use the Replace command to remove the unwanted spaces. This command works similarly as the Substitute function. The keyboard shortcut is Ctrl + H
Replace command can be used in case we know our text contains spaces in between the text we need to remove them.
Find what: write space by pressing the space bar key once.
In Replace with: NULL
We cannot see the entry in the Find What box because we have put a space there. Click on Replace All till you see the dialog box showing the text that it could not find any more spaces to replace.
You will get the result below:
So this is how you can remove the leading and trailing spaces and spaces in between the text and make your data more readable.
Remove Trailing Space through VBA
In case you want a VBA code to remove all the trailing spaces in only the active sheet then this article will definitely help you. Many times we leave a space at the end in error or sometimes we copy data from the web & do not notice. In this article, we will learn how to eliminate the space left in error.
Question): In Excel, we can use TRIM function with SUBSTITUTE & CHAR formula i.e. =TRIM(SUBSTITUTE(A1,CHAR(160)," ")) to remove trailing space at the end of any text.
I would like a Macro to help me in removing all the unnecessary spaces left at the end of the string.
To get the code for removing space at the end of the text; we need to follow the below steps to launch VBA editor.
Copy the following code in worksheet module
Dim c As Range, rngConstants As Range
On Error Resume Next
Set rngConstants = ActiveSheet.UsedRange.SpecialCells(2, 2)
On Error GoTo 0
If Not rngConstants Is Nothing Then
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'trim cells incl char 160
For Each c In rngConstants
c.Value = Trim$(Application.Clean(Replace(c.Value, Chr(160), " ")))
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
As you will This will remove all the spaces at the end.
Note: The above macro will run only on activesheet.
In this way, we can remove the trailing spaces at the end, using VBA code.
Hope this article about How to remove Unnecessary Empty Characters in Excel is explanatory. Find more articles on calculating values and related Excel formulas here. If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook. We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write to us at email@example.com.
Related Articles :
How to Use TRIM function in Excel: The TRIM function is used to trim strings and clean any trailing or leading spaces from string. This helps us in the cleaning process of data a lot.
How to use the CLEAN function in Excel: Clean function is used to clean up unprintable characters from the string. This function is mostly used with the TRIM function to clean up imported foreign data.
Replace text from end of a string starting from variable position: To replace text from the end of the string, we use the REPLACE function. The REPLACE function uses the position of text in the string to replace.
Excel REPLACE vs SUBSTITUTE function: The REPLACE and SUBSTITUTE functions are the most misunderstood functions. To find and replace a given text we use the SUBSTITUTE function. Where REPLACE is used to replace a number of characters in string…
Replace text from end of a string, starting from variable position: To replace text from the end of the string, we use the REPLACE function. The REPLACE function uses the position of text in the string to replace.
Popular Articles :
50 Excel Shortcuts to Increase Your Productivity : Get faster at your tasks in Excel. These shortcuts will help you increase your work efficiency in Excel.
How to use the VLOOKUP Function in Excel : This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets.
How to use the IF Function in Excel : The IF statement in Excel checks the condition and returns a specific value if the condition is TRUE or returns another specific value if FALSE.
How to use the SUMIF Function in Excel : This is another dashboard essential function. This helps you sum up values on specific conditions.
How to use the COUNTIF Function in Excel : Count values with conditions using this amazing function. You don't need to filter your data to count specific values. Countif function is essential to prepare your dashboard.
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.