Count the number of Xs in a cell

Manny74

Board Regular
Joined
May 6, 2016
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello All,

I need a formula that will allow me to count the number of Xs in a cell....The total Xs should be 5, but when I use the formula, COUNTA(B2:M2) , it counts 3.... I tried COUNTIF(B2:M2,"X") but it also counted only 3....

What formula can I use to count the number of Xs in each cell?



A BCDEFGHIJKLM
1Shift123456789101112
27 AM XXX X X

<colgroup><col><col><col span="12"></colgroup><tbody>
</tbody>
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,051
Re: How to count the number of Xs in a cell

Try
=SUMPRODUCT(LEN(B2:M2)-LEN(SUBSTITUTE(B2:M2,"x","")))
 

Manny74

Board Regular
Joined
May 6, 2016
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Re: How to count the number of Xs in a cell

I tried it, but got 5...So it worked, thanks!
But is there a simpler formula to use?
I was hoping to keep it simple for my employees :)
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,051
Re: How to count the number of Xs in a cell

That's pretty much as simple as I can get it.
If there is a maximum number of X's in a cell you could use something like =COUNTIF(B2:M2,"X")+COUNTIF(B2:M2,"XX")+COUNTIF(B2:M2,"XXX"), but I don't know if that is "simpler".

Also, where do the X come from. If you want to enter numbers in the cells, there is conditional formatting that will result in a DataBar to give some visual cue.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,249
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Re: How to count the number of Xs in a cell

Your could create your own User Defined Function in VBA to do it. Then, they would just need to use the function you created.
For example, if we created the Function and named it CountX, then they would just use the formula:
=CountX(B2:M2)

You can either create that function in VBA in the exact workbook they will be using, or you could create it as part of an Add-In.
If it is in the workbook, then they just need to be sure to use that particular workbook, and enable VBA for that workbook.
If created as part of an Add-In, then they would need to install the Add-In on the computer.

Along with Mike's formula, those are your options for making it simple for your users.
 
Last edited:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,051
Re: How to count the number of Xs in a cell

Similar to Joe4's suggestion, you could also use Named Formulas.

If you want the sum of X's from row 2 in a cell. Select that cell and define the name

Name: COUNTXS
RefersTo: =SUMPRODUCT(LEN($B2:$M2)-LEN(SUBSTITUTE($B2:$M2,"x","")))

(note the mix of absolute and relative addressing)

Then, when you put =COUNTXS in that cell it will return the number of x's in B2:M2
Drag the formula down one row and it will return the x's from B3:M3, etc.
 

Manny74

Board Regular
Joined
May 6, 2016
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Re: How to count the number of Xs in a cell

The X represents the number of times we are using restraints on clients.
I thought about using numbers, but that would mean I'd have to retrain several, several staff members to use number instead of Xs.....
Hmmm I am having trouble with that formula =COUNTIF(B2:M2,"X")+COUNTIF(B2:M2,"XX")+COUNTIF(B2:M2,"XXX")
Let me try again....
 

Manny74

Board Regular
Joined
May 6, 2016
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I see what it's doing...
When I use this formula:

=COUNTIF(B2:M2,"X")+COUNTIF(B2:M2,"XX")+COUNTIF(B2:M2,"XXX")

It counts the number of cells with 1 X, 2 Xs & 3 Xs...

But it wont the number of Xs in each cell.....
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,249
Office Version
  1. 365
Platform
  1. Windows
But it wont the number of Xs in each cell.....
Use Mike's original formula to get the total number of X's.
Note that if the "X"s are all upper case, you will need to adjust his formula to look for "X" instead of "x".
 

Manny74

Board Regular
Joined
May 6, 2016
Messages
103
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Re: How to count the number of Xs in a cell

That is another good idea...
I have MS Excel 2013, so how would I create a user defined function in VBA?
 

Forum statistics

Threads
1,136,315
Messages
5,675,038
Members
419,546
Latest member
RobWayCot

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
Top