Counting rows with criteria

  • Thread starter Thread starter tsingsiu
  • Start date Start date
T

tsingsiu

Hello,

I've been looking for a solution to this problem, I need to get a count
of all rows that have an "X", but not the total number of "X" in the
range. Any solutions or tips? Thanks!

A B C D
1 / / / /
2 X X X X
3 / / / /
4 / / X /
5 X X X X
6 / X / /
7 X X X X
8 X X X X
9 / / / X
10 / / X X
11 / / / /
12 / / / /
13 / / / /
14 / / / X
15 / / / /
16 X X X X
17 / X / /
18 / / / /
19 / / / /
20 / / X /

TKO
 
=SUMPRODUCT(--(COUNTIF(OFFSET(A1,ROW(A1:A25)-1,,1,2),"*X*")>0))

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
Try this

Sub Example1()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim num As Long

Firstrow = ActiveSheet.UsedRange.Cells(1).Row
Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

num = 0

With ActiveSheet
For Lrow = Lastrow To Firstrow Step -1

If Application.WorksheetFunction.CountIf(.Rows(Lrow), "x") > 0 Then num = num + 1

Next
End With

MsgBox num

End Sub
 
The tips have been very helpful!

For the formula that Bob provided,
=SUMPRODUCT(--(COUNTIF(OFFSET(A1,ROW(A1:A25)-1,,1,2),"*X*")>0)), which
variables need to be updated if I move my data, say from A1:E25 to
C12:G37 (I need to insert some columns and rows surronding the table of
data)?

Thank you!
TKO
 
=SUMPRODUCT(--(COUNTIF(OFFSET(C12,ROW(A1:A25)-1,,1,2),"*X*")>0))

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 

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

Back
Top