I would like to count all special characters (!%_*?+-,) in a cell.
For example:
With this formula =LEN(D2)-LEN(SUBSTITUTE(D2;"!";""))
i can only count one character, but i need to count multiple characters in single cell...is there a way how to tweak this?
Thanks for the help!
=8*LEN(D2)-LEN(SUBSTITUTE(D2;"!";""))-LEN(SUBSTITUTE(D2;"%";""))-LEN(SUBSTITUTE(D2;"_";""))-LEN(SUBSTITUTE(D2;"*";"")) -LEN(SUBSTITUTE(D2;"?";""))-LEN(SUBSTITUTE(D2;"+";""))-LEN(SUBSTITUTE(D2;"-";""))-LEN(SUBSTITUTE(D2;",";""))
You can do this with a simple array formula:
Special_Characters
is a range listing all your special characters. You could manually enter them as an array constant if you prefer:but the named range seems simpler.
To array-enter a formula, after entering the formula into the cell or formula bar, hold down ctrl + shift while hitting enter. If you did this correctly, Excel will place braces
{...}
around the formula.If you prefer a VBA solution, I would suggest the code below. You will need to modify
.Pattern
to include any other characters you do NOT want to count. In the code below, any character that is not an upper or lower case letter, or a digit, will be counted as a special character.Using formulas only, not VBA, this is possible with the following two approaches:
Consider the text in
A1
:First approach:
Multiple nested
SUBSTITUTE
:Second approach:
Using
SUMPRDUKT
for getting theMID
function in array context to get each single charachter of the string inA1
compared with each single special character:Here is a simple version I created:
Two points:
References > Microsoft VBScript Regular Expressions 5.5
=CountSpecialCharacters(A2)