Home » excel » Excel: Counting multiple characters by cell in range

# Excel: Counting multiple characters by cell in range

Questions:

I have a range as A:A since the amount of records changes. This range contains “x” in each cell which corresponds to how many projects were completed in that time. Therefore, a single cell may be entered as “x”, “xxx”, or “x x x”. I need the number of occurrences of “x” or “X” for that matter.

My current formula is `=COUNTIFS(A:A,"x*",I:I,"Weekly*")`I have tried altering this formula but can’t get the proper result as some cells contain more than one “x”. Thanks.

``````=SUM(LEN(A:A)-LEN(SUBSTITUTE(SUBSTITUTE(A:A,"x",""),"X","")))
``````

Substitute `x` & `X` with a blank value and then count & sum the difference. Depending on your version of excel, you may need to confirm formula with CTRL + SHIFT + ENTER

## Edit

A cleaner way to handle lower case and uppercase values would just be to convert the entire string to uppercase and then only replace your uppercase values (`X`)

``````=SUM(LEN(A:A)-LEN(SUBSTITUTE(UPPER(A1),"X","")))
``````

i have use a helper column. Modify and try the below:

Formula in Helper Column:

`=LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"X",""))`

Formula to get the total:

`=SUMIF(\$B\$2:\$B\$8,"Weekly*",\$C\$2:\$C\$8)` Assuming that Column A only contains “X”s, and no other data, then the simplest method – if you have Office-365 or Office-2019 is to use `Concat` to turn the column into 1 giant string, then count the length:

``````=LEN(CONCAT(A:A))
``````

If you don’t have Office-365 or Office-2019, then you can’t use the `Concat` function — as such, you will need to use an Array Formula to add the lengths:

``````=SUMPRODUCT(LEN(A:A))
``````

(Using `SUMPRODUCT` instead of `SUM` means that we can press Enter as a normal formula, and everything inside the `SUMPRODUCT` is treated as an Array Formula, instead of having to press Ctrl+Shift+Enter)

As BigBen has pointed out, you have said that there are sometimes spaces in there. You have 2 ways to deal with this: either `SUBSTUTUTE` out the Spaces, or `SUBSTITUTE` out the Xs and take the difference

``````=LEN(SUBSTITUTE(CONCAT(A:A)," ",""))
=SUMPRODUCT(LEN(SUBSTITUTE(A:A," ","")))
'OR
=LEN(CONCAT(A:A)) - LEN(SUBSTITUTE(LOWER(CONCAT(A:A)),"x",""))
=SUMPRODUCT(LEN(A1:A4))-SUMPRODUCT(LEN(SUBSTITUTE(LOWER(A1:A4),"x","")))
``````

(The `LOWER` is needed, because `SUBSTITUTE` is Case Sensitive: `=SUBSTITUTE("|XxX|","x")` gives `|XX|`, not `||`)