How to count the total number of leading and trailing spaces in cells?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to come up with a way to count leading and trailing spaces in cells. I have seen posts discussing ways of trimming such spaces, but I didn't see anything about counting them.

(The reason I need this is that it will help me identify the source software from which some of my data comes from. When students copy and paste data from the source, cells get pasted with leading and/or trailing spaces in addition to other source formatting etc.)

Thanks for any input! 😀

P.S. By the way, by "total" in the title, I didn't mean the sum of leading and trailing space counts. I need them separately.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
=len(text)-len(trim(text))
sorry, didn't read the PS.
Map2
ABCD
1 abcde fghi 4leading22
28trailing10
312
Blad1
Cell Formulas
RangeFormula
B1B1=SEARCH(LEFT(TRIM(A1),1),A1,1)-1
B2B2=LEN(A1)-B1-LEN(TRIM(A1))
D1D1=LEN(A1)
D2D2=LEN(TRIM(A1))
D3D3=+D1-D2
 
Last edited:
Upvote 0
Sorry for my misunderstanding in previous post.
If A2 could be:
"abc" => 0
" abc " => 1 + 1
" a bc " => 1 + 1 (not count space inside a & bc)

B2 (count leading spaces):
=MATCH(TRUE,INDEX(CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1))<>32,),0)-1

C2 (count trailing spaces):
=LEN(A2)-AGGREGATE(14,6,ROW(INDIRECT("1:"&LEN(A2)))/(CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1))<>32),1)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,604
Messages
6,173,320
Members
452,510
Latest member
RCan29

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top