# Counting a character in a cell that contains a string

#### maic15

##### Active Member
I have an excel spreadsheet where the structure cannot change. I have cells that contain data that I need to count.
For example,
Cell Tange: B1:b45
O,FG,CJP
O,FG,CJP
O,FG,CJP

I need to count "O" in each cell. I tried Countif(Left(B1:B45,"O"),1) and it did not work.
Thoughts?

### Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try

=SUMPRODUCT(--(LEFT(B1:B45,1)="O"))

maic15 said:
I have an excel spreadsheet where the structure cannot change. I have cells that contain data that I need to count.
For example,
Cell Tange: B1:b45
O,FG,CJP
O,FG,CJP
O,FG,CJP

I need to count "O" in each cell. I tried Countif(Left(B1:B45,"O"),1) and it did not work.
Thoughts?

If

=COUNTIF(B1:B45,"*O*")

does not fit the bill, maybe because you might be needing something like:

=SUMPRODUCT(--ISNUMBER(SEARCH(",O,",","&B1:B45&",")))

I wrote the post wrong...The "O" does not always apear first in the cell. I need to have code if it appears in another part of the string.

=SUMPRODUCT(--ISNUMBER(SEARCH(",O,",","&B1:B45&",")))

Yes..But it only returend 11 and the total is 12..The "O" can appear in mutiple places

I need to have code if it appears in another part of the string

If:
1. O will only appear once in any given cell, and
2. I understand what you're after...
maybe something like this will help:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CountO()
<SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, c <SPAN style="color:#00007F">As</SPAN> Range, Rng <SPAN style="color:#00007F">As</SPAN> Range
<SPAN style="color:#00007F">Set</SPAN> Rng = Range("B1:B45")
i = 0
<SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Rng
<SPAN style="color:#00007F">If</SPAN> InStr(c, "O") <SPAN style="color:#00007F">Then</SPAN> i = i + 1
<SPAN style="color:#00007F">Next</SPAN> c
MsgBox i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope it helps,

(Hi Aladin. Nothing to report yet... )
Dan

=SUMPRODUCT((LEN(B1:B45)-LEN(SUBSTITUTE(UPPER(B1:B45),"O",""))))

Replies
4
Views
427
Replies
3
Views
419
Replies
2
Views
436
Replies
9
Views
180
Replies
1
Views
433

1,218,788
Messages
6,144,498
Members
450,549
Latest member
desperato

### 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.

### Which adblocker are you using?

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

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