Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of spaces before a string in Excel

Tags:

excel

A program that exports to Excel creates a file with an indented list in a single column like this:

Column A
 First Text
  Second Text
   Third Text
  Fourth Text
 Fifth Text

How can I create a function in excel that counts the number of white spaces before the string of text?

So as to return: 1 for the first text row and 3 for the for the thirst row, etc in this example.

Preferably seeking a non-VBA solution.

like image 967
AME Avatar asked Feb 19 '14 07:02

AME


3 Answers

TRIM doesn't help here because it removes double spaces also between words.

The main idea is to find the FIRST letter in the trimmed string and find its position in the original string:

=FIND(LEFT(TRIM(A1),1),A1)-1
like image 149
valex Avatar answered Oct 02 '22 01:10

valex


You can try this function in Ms Excel itself:

=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))

This would apply if the results are in a single cell. If it is for a whole row/column, just drag the formula accordingly.

like image 44
maroon Avatar answered Oct 02 '22 02:10

maroon


Try below:

=FIND(" ",A1,1)-1

It calculates the position of the first found whitespace character in a cell and reduces it by 1 to reflect number of characters before that position.

like image 33
HIM Avatar answered Oct 02 '22 02:10

HIM