How to Count Spaces before the Text String

,

This post will guide you how to count the number of spaces before the text string in a cell in Excel. How to count the leading spaces before the text string in one cell with formula in excel. How do I create an excel formula that counts the number of spaces before the text string.

1. Count number of spaces before a string using a Formula

Assuming that you have lots of cells contain text string with different levels of indentation on them. You want to count the number of spaces before each test string in cells, and then you want to know how to count the number of spaces before the text string in each cell.

You can create an excel formula based on the FIND function, the LEFT function, and the TRIM function.

For example, let’s count the number of spaces before text string in Cell B1, you can write down the following excel formula:

=FIND(LEFT(TRIM(B1),1),B1)-1
count spaces before text1

Let’s see how this formula works:

The TRIM function can be used to remove all spaces at the start or end of the text string, then you can get a text string without any spaces. The returned result will pass into the LEFT function to get the leftmost characters of the result without spaces.

Then use the FIND function to search for that characters in the original string, and it returns the position of the first character of the searching characters in the original string. The result is subtracted 1 to get the number of spaces before the text string in Cell B1.

When you use this formula, you do not need to care how many other spaces there are or where they are.

If there are no spaces at the end of the text string in each cell, then you also can use the following formula:

=LEN(B1)- LEN(TRIM(B1))
count spaces before text2

2. Count Spaces before the Text String using User Defined Function with VBA Code

You can create a user-defined function in Excel using VBA (Visual Basic for Applications) to count the number of spaces before a text string. Just do the following steps:

Step1: Press Alt + F11 to open the Visual Basic Editor.

Step2: Click Insert > Module to insert a new module.

Step3: Type the following code into the module:

How to Count Spaces before the Text String vba 1.png
Function CountSpaces_Excelhow(str As String) As Integer
    Dim i As Integer
    For i = 1 To Len(str)
        If Mid(str, i, 1) <> " " Then
            Exit For
        End If
    Next i
    CountSpaces_Excelhow = i - 1
End Function

Step4: Enter the formula into a cell, then press Enter key to apply it.

=CountSpaces_Excelhow(B1)
How to Count Spaces before the Text String vba 2.png

3. Video: Count Spaces before the Text String

This video will show you how to count spaces before the text string in Excel using a formula or VBA code.

4. Related Formulas

  • Get first word from text string
    If you want to extract the first word from a text string in a cell, you can use a combination of the IF function, the ISERR function, the LEFT function and the FIND function to create a complex excel formula..…
  • Get last word from text string
    If you want to get the last word from a text string, you can create an excel formula based on the RIGHT function, the LEN function, the FIND function and the SUBSTITUTE function..…
  • Extract nth word from text string
    If you want to extract the nth word from a text string in a single cell, you can create an excel formula based on the TRIM function, the MID function, the SUBSTITUTE function, the REPT function and the LEN function..…
  • count specific words in a cell or a range
    If you want to count the number of a specific word in a single cell, you need to use the SUBSTITUTE function to remove all that certain word in text string, then using LEN function to calculate the length of the substring that without that specific word.…

5. Related Functions

  • Excel LEFT function
    The Excel LEFT function returns a substring (a specified number of the characters) from a text string, starting from the leftmost character.The LEFT function is a build-in function in Microsoft Excel and it is categorized as a Text Function.The syntax of the LEFT function is as below:= LEFT(text,[num_chars])…t)…
  • Excel FIND function
    The Excel FIND function returns the position of the first text string (sub string) within another text string.The syntax of the FIND function is as below:= FIND(find_text, within_text,[start_num])…
  • Excel LEN function
    The Excel LEN function returns the length of a text string (the number of characters in a text string).The syntax of the LEN function is as below:= LEN(text)…
  • Excel TRIM function
    The Excel TRIM function removes all spaces from text string except for single spaces between words.  You can use the TRIM function to remove extra spaces between words in a string.The syntax of the TRIM function is as below:= TRIM (text)….

Leave a Reply