How to Count Space Before Text in Excel (4 Ways)

We will be using the following dataset. As you can see from the picture, there are different numbers of spaces before the text in each cell, and we will count their number.


Method 1 – Combining the TRIM Function with LEFT and FIND Functions

Steps:

  • Select cell C5.
  • Insert the following formula.
=FIND(LEFT(TRIM(B5),1),B5)-1

how to count space before text in excel using find, left and trim functions

  • Press Enter.

  • Select the previous cell and click and drag the Fill Handle Icon down to the end of the list to fill up the rest of the dataset.

counting space before text in excel

Breakdown of the Formula:

TRIM(B5) takes the text from cell B5, removes extra spaces, and gives the output Excel.

LEFT(TRIM(B5),1) takes Excel as the argument and takes out the first character from the text, E.

FIND(LEFT(TRIM(B5),1), B5) function finds the first instance of E in the text, which is at the first position.

FIND(LEFT(TRIM(B5),1), B5)-1 returns the amount of space before it, which, in this case, is zero. But for other texts, there are values.

Read More: How to Count Alphabet in Excel Sheet


Method 2 – Count Spaces Before Text Using the SEARCH and TRIM Functions

Steps:

  • Select cell C5.
  • Insert the following formula in the cell.
=SEARCH(TRIM(B5),B5)-1

how to count space before text in excel using search and trim functions

  • Press Enter on your keyboard and you will have the result for the first cell.

  • Click and drag the Fill Handle Icon down to the end to find out the number of spaces for the rest of the cells.

dragging auto fill icon down

Breakdown of the Formula:

TRIM(B5) takes value from cell B5 as input and removes all the unnecessary spaces between texts, and in this case returns Excel.

SEARCH(TRIM(B5), B5) takes Excel and cell B5 as input arguments and searches the first character of the first argument in the second argument. Here, it is returning the position of E in cell B5, which is 1.

As SEARCH(TRIM(B5), B5) returns the first position where the text starts SEARCH(TRIM(B5), B5)-1 finally returns the number of spaces before it in the text. In this context, it is zero, but there are other values for different cells.

Read More: How to Count Characters in Cell Including Spaces in Excel


Method 3 – Applying LEN and SUBSTITUTE Functions

Steps:

  • Select cell C5.
  • Insert the following formula in the cell.
=LEN(B5)-LEN(SUBSTITUTE(B5," ",""))

how to count space before text in excel using len and substitute functions

  • Press Enter on your keyboard. You will get the number of spaces in cell B5.

  • Drag down the Fill Handle Icon to fill up the rest of the cells with the formula for their respective cells.

dragging down len and substitute function

Breakdown of the Formula:

SUBSTITUTE(B5,” “,””) removes all the spaces in cell B5.

LEN(SUBSTITUTE(B5,” “,””)) returns the length of cell B5 without any spaces.

LEN(B5) returns the length of cell B5.

LEN(B5)-LEN(SUBSTITUTE(B5,” “,””)) returns the difference between the total length of the text and the length of text without space.

This formula won’t work if there are other spaces apart from the starting ones.

Read More: How to Count Characters in Cell without Spaces in Excel


Method 4 – Embedding VBA Code to Count Spaces Before Text

To do this, you need the Developer tab on your ribbon. If you don’t have it, you have to enable the Developer tab.

Steps:

  • Select the Developer tab from the ribbon, then select Visual Basic from the Code group.

  • Click on Insert, then select Module from the drop-down list.

inserting module in vba to count space before text in excel

  • In the new module, insert the following code.
Function NumSpacesStart(str As Variant) As Integer

Dim trimmed As String

trimmed = LTrim(str)

NumSpacesStart = InStr(1, str, Left(trimmed, 1), vbTextCompare) - 1

End Function
  • Save the code.
  • Close the VBA window and select your cell.
  • Insert the following formula in the cell.
=NumSpacesStart(B5)

entering formula created with vba

  • Press Enter. You will get the number of spaces in cell B5.

  • Click and drag the Fill Handle Icon to fill up the formula for the rest of the cells. You will get the number of spaces in each cell before the text.

filling up vba formula with auto fill

Read More: Excel VBA: Count Characters in Cell


Download the Practice Workbook


Related Articles


<< Go Back to Count Characters in Cell | String Manipulation | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Abrar-ur-Rahman Niloy
Abrar-ur-Rahman Niloy

Abrar-ur-Rahman Niloy, holding a B.Sc. in Naval Architecture and Marine Engineering, has contributed to Exceldemy for nearly 1.5 years. As a leader in Excel, VBA, and Content Development teams, he authored 114+ articles and assisted the Exceldemy forum. Presently, as a project writer, he prioritizes stepping out of his comfort zone, aiming for constant technical improvement. Niloy's interests encompass Excel & VBA, Pivot Table, Power Query, Python, Data Analysis, and Machine Learning libraries, showcasing his commitment to diverse... Read Full Bio

1 Comment
  1. Hello, Thank you for making this post on how to create a function to count the number of words in an excel cell.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo