Complete Guide to Counting Characters in Excel (LEN/LENB, Excluding Spaces, Specific Characters/Words, Range Totals, Emoji/Line Breaks)

Excel character count — Complete Guide to Counting Characters

Excel character count is basically handled with the LEN function. In practice, you may also need to exclude spaces, remove line breaks, count specific characters or words, total an entire range, and count bytes with LENB.

Quick Fix

=LEN(A2)                                             
=LEN(SUBSTITUTE(A2," ",""))                            
=LEN(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,CHAR(160)," "),CHAR(10)," "),CHAR(13)," ")))) 
=LEN(A2)-LEN(SUBSTITUTE(A2,"a",""))                    
=IF(TRIM(A2)="",0, LEN(TRIM(A2))-LEN(SUBSTITUTE(TRIM(A2)," ",""))+1)  
=SUMPRODUCT(LEN(A2:A100))                              

LEN vs LENB

LEN returns the number of characters, while LENB returns the number of bytes (in DBCS environments). If you need byte calculations, verify the behavior in your environment.

Practical Examples

Handling Spaces and Line Breaks

=LEN(TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A4,CHAR(10)," "),CHAR(13)," "))))

Count Specific Characters or Words

=LEN(A2)-LEN(SUBSTITUTE(A2,"o",""))
=LEN(LOWER(A2))-LEN(SUBSTITUTE(LOWER(A2),"a",""))
=(LEN(A5)-LEN(SUBSTITUTE(A5,"Apple","")))/LEN("Apple")

Total Length Across a Range

=SUMPRODUCT(LEN(A2:A100))

Length by Row or Column

=BYROW(A2:D10, LAMBDA(r, SUM(LEN(r))))
=BYCOL(A2:D10, LAMBDA(c, SUM(LEN(c))))

Byte Count (LENB)

=LENB(A5)

Checklist

  • Exclude spaces: SUBSTITUTE(," ","") or TRIM
  • Remove line breaks/tabs: Handle CHAR(10/13/9)
  • Specific characters/words: LEN - LEN(SUBSTITUTE())
  • Range total: SUMPRODUCT(LEN(range))
  • Bytes: LENB (verify in your environment)

Troubleshooting

SymptomCauseSolution
Length is too highIncludes NBSP/line breaksTRIM+CLEAN+SUBSTITUTE(CHAR(160))
Incorrect word countMultiple spaces/tabsClean up with TRIM and CLEAN
Specific character count is 0Case mismatchNormalize with LOWER/UPPER
Different LENB valuesDBCS/platform differencesTest samples in the target environment

Related Articles


Official documentation (recommended): Microsoft: LEN · Microsoft: LENB

Leave a Reply

Your email address will not be published. Required fields are marked *