Counting rows based on a condition

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have a table called 'tblSoftwareProducts' which contains 2 columns i.e. softwareproductname and chosenproduct.
Typical generic data could be something like the following

softwareproductname chosenproduct
Product1 Yes
Product2 No
Product3 Yes

I am looking for some VBA code that counts the number of rows, less the header, where the chosenproduct is equal to Yes.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Perhaps you're after something like this ...

VBA Code:
Public Sub azizrasul()
    Dim i As Long
    Application.ScreenUpdating = False
    With ActiveSheet.ListObjects("tblSoftwareProducts").Range
        .AutoFilter Field:=2, Criteria1:="yes"
        i = .SpecialCells(xlCellTypeVisible).Rows.Count
        .AutoFilter
    End With
    Application.ScreenUpdating = True
    MsgBox i
End Sub
 
Upvote 0
Another option
VBA Code:
Sub azizrasul()
   MsgBox Evaluate("COUNTIF(tblSoftwareProducts[chosenproduct],""yes"")")
End Sub
 
Upvote 0
Solution
Another option
VBA Code:
Sub azizrasul()
   MsgBox Evaluate("COUNTIF(tblSoftwareProducts[chosenproduct],""yes"")")
End Sub

Thanks for that.

GWteB, your solution gave me an extra count by 1. I had a header plus two records with YES and I got a count of 3 instead of 2.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
GWteB, your solution gave me an extra count by 1. I had a header plus two records with YES and I got a count of 3 instead of 2.
Not sure what went wrong since it gave me under different conditions always the correct count.
Glad Fluff's code works for you.
 
Upvote 0
@GWteB
You code will include the header row and will only count the number of rows until the 1st No. So if the first data row has a No, your code will return 1, regardless of the number of visible rows.
 
Upvote 0
My pleasure.
FYI your approach will work if you do it like
VBA Code:
        i = .Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,619
Members
449,039
Latest member
Mbone Mathonsi

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