# 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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

#### Oaktree

##### MrExcel MVP
Try

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

##### MrExcel MVP
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&",")))

#### maic15

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

#### Oaktree

##### MrExcel MVP

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

#### maic15

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

#### HalfAce

##### MrExcel MVP
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

#### Todd Bardoni

##### Well-known Member
=SUMPRODUCT((LEN(B1:B45)-LEN(SUBSTITUTE(UPPER(B1:B45),"O",""))))

Replies
7
Views
333
Replies
2
Views
167
Replies
1
Views
91
Replies
4
Views
295
Replies
5
Views
1K

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

1,151,575
Messages
5,765,188
Members
425,266
Latest member
CPAgirl

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