Counting a character in a cell that contains a string

maic15

Active Member
Joined
Nov 17, 2004
Messages
313
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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&",")))
 
Upvote 0
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.
 
Upvote 0
Did you try Aladin's?

=SUMPRODUCT(--ISNUMBER(SEARCH(",O,",","&B1:B45&",")))
 
Upvote 0
Yes..But it only returend 11 and the total is 12..The "O" can appear in mutiple places
 
Upvote 0
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... :biggrin: )
Dan
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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
Back
Top