How do you count specific values in a row

R

RLN

RE: Excel 2003
I have a row of data in Excel that is in Row 4 from Column B to Column AF.

In this row, some cells contain the value "X", some contain the value "O",
and yet other cells in this row will contain other text data.

I tried the COUNTA function but that counts every cell that has a non-null
value. I need more spoecific information than that. I also tried it with an
immediate if, but somehow got the syntax incorrect.

Per my example above, is there a way in VBA to count across row 4 (from cell
B4 to cell AF4 how many cells contain the value of "X" and place that in cell
A5 and count how many cells contain the value of "O" and place that value in
cell A6?


Thanks.
 
R

Rick Rothstein

You don't need VBA for this; just use the COUNTIF function.

A5: =COUNTIF(B4:AF4,"X")

A6: =COUNTIF(B4:AF4,"O")

Just put the text you want to find into the last argument.
 
G

Gary''s Student

Sub countX()
Set r = Range("B4:AF4")
Set a6 = Range("A6")
Set a5 = Range("A5")
x = "X"
o = "O"
a5.Value = Application.WorksheetFunction.CountIf(r, x)
a6.Value = Application.WorksheetFunction.CountIf(r, o)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top