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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,210
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
Joined
Nov 17, 2004
Messages
313
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
Joined
Jun 20, 2002
Messages
8,105
Office Version
  1. 365
Did you try Aladin's?

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

maic15

Active Member
Joined
Nov 17, 2004
Messages
313
Yes..But it only returend 11 and the total is 12..The "O" can appear in mutiple places
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,456
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
 

Forum statistics

Threads
1,181,166
Messages
5,928,489
Members
436,604
Latest member
nomi_nomi

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