In this article, we will discuss doing left trim in Excel using both function and VBA. Left trim can be of various types. For example, you can remove a character from the left side of a string. On the other hand, you can also erase leading spaces (Space present on the leftmost side of a string) from a data string.
Download Practice Workbook
You can download our practice workbook that we’ve used to prepare this article.
7 Suitable Ways to Function Left Trim in Excel
1. Apply RIGHT Function to Trim Left Side Characters in Excel
When we work with spreadsheets, often, we need to trim the data from the left. For instance, you have a code containing color, reference no, size, etc. and you want to separate the code according to your preference. The easiest way to trim the left portion of data is to use the RIGHT function. The RIGHT function returns the specified character from the end of the text string. For instance, we want to trim the value of Cell B5. Here are the steps required:
- Write the following formula to separate the color from the code.
- After entering the formula, the color Blue will be separated. Use the Fill Handle (+) to copy the formula to the rest of the cells.
2. Application of REPLACE Function to Remove Left Side Characters in Excel
Another easy to left trim is to use the REPLACE function. REPLACE function replaces part of a text string with a different text string. We have applied the function to the previous set of data and here are the steps followed:
- Firstly, type the following Formula:
- Upon entering the formula, here is the result we got:
3. Use VBA to Trim Left Side Characters
If you do not want to use any function or formula to do the left trim, using VBA can be an option in excel. We have used the following steps for the VBA:
- Following is the string we want to trim:
- First, go to the corresponding sheet. Then, right-click on the sheet name and select the View Code option to bring the VBA window.
- Now, write the following code in the Module.
Option Explicit Sub GetString() Dim str1 As String str1 = "3PastelBlue" MsgBox Right(str1, 10) End Sub
- Finally, run the code and you’ll get the code trimmed.
4. Delete Characters from Left Using RIGHT and LEN Function
Likewise, methods mentioned above, we can remove characters from the left part of the data by using the combination of RIGHT and LEN functions. The LEN function returns the number of characters in a text string. Steps associated with this method are:
- At first, type the following formula.
Breakdown of the Formula:
Here, the LEN function returns the number of characters in a text string.
In this formula, the number of characters trimmed from the left is subtracted from the whole string. The formula will return the string trimmed from the left.
- Finally, the output is:
5. Remove Leading Spaces Using the Combination of FIND, MID, TRIM & LEN Functions in Excel
Often, when we copy data from websites, many unnecessary spaces are also copied with the data. But, finding these spaces one by one will be time-consuming. Fortunately, there are some combinations of functions available to trim these spaces.
In this method, we will use the combination of FIND, MID, TRIM & LEN functions to remove the leading space. The steps involved are:
- To remove the spaces, type the following formula:
Breakdown of the Formula:
This formula will return the number of characters in Cell B5.
- TRIM (B5)
The TRIM function removes all spaces from B5 except for single spaces between words.
The MID function returns characters from the middle of B5, given the starting position and length.
The FIND function returns the starting position of one text string within another text string. Here the formula returns the position of the character we found in the previous procedure.
When applying the formula with another MID function, it will erase the spaces from only the left side of the string.
- In the end, the output is:
Read More: How to Trim Spaces in Excel (8 Easiest Ways)
6. Apply Combination of REPLACE, LEFT, FIND & TRIM Excel Functions to Remove Leading Spaces
Likewise, the previous method, the combination of REPLACE, LEFT, FIND, and TRIM functions can be used to erase leading spaces.
We have followed the below steps to complete the task:
- Using the abovementioned functions, type the following formula first:
Breakdown of the Formula:
The combination of FIND, LEFT, and TRIM functions help us to calculate the position of the first space character in the string; spaces towards the left side of the string.
Here, we passed the formula through REPLACE function. As a result, the leading spaces of the string were replaced with no blank (“”). The formula will erase only the leading spaces from the string.
- Lastly, The result is:
7. Use VBA to Remove Leading Spaces in Excel
Leading spaces can be deleted using the VBA also.
- Firstly, select the cells with leading spaces.
- Secondly, go to the corresponding sheet, right-click on the sheet name, and select the View Code option to bring the VBA window.
- In the Module, write the following code:
Option Explicit Sub TrimExample1() Dim Cell As Object Dim Rng As Range Set Rng = Selection For Each Cell In Rng Cell.Value = Trim(Cell) Next Cell End Sub
- Lastly, run the code and get the string without leading spaces.
In the article, I have discussed widely used methods to trim the left characters. You can learn both about the methods related to function and VBA. Use these methods, whenever suitable, and let us know if you have any queries.