Count visible cells criteria in VBA

Av8tordude

Well-known Member
Joined
Oct 13, 2007
Messages
1,074
Office Version
  1. 2019
Platform
  1. Windows
I've done multiple search combination to find the answer, but unfortunately nothing matches what I looking for. I'm auto-filtering my data and I would like to count the number cells that is greater than 0 in VBA. Can someone assist.

Thank you kindly
 
Hi Rick,

the output results needs to be displayed in a userform label. Can you tell me how to display it in a userform label? Thank you for help
First off, here is new code taking into account your specified column (P) and desire to be able to count numbers greater than and also less than zero...
Code:
[table="width: 500"]
[tr]
	[td]Function ZeroCount(Symbol As String)
  Dim Ar As Range
  For Each Ar In Range("P18", Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlVisible).Areas
    ZeroCount = ZeroCount + WorksheetFunction.CountIf(Ar, Symbol & "0")
  Next
End Function[/td]
[/tr]
[/table]
The argument you pass to the function is either "<" to count cells less than 0 or ">" to count cells greater than 0. You can also pass in "=" if you wanted to count the number of zeroes as well. The function assumes the worksheet you want to count is the active worksheet (this is in keeping with the code you posted so far). To use this function in your UserForm, simply assign the function to the Label's Caption property. For example,

Label1.Caption = ZeroCount(">")

to count cells greater than 0; or...

Label2.Caption = ZeroCount("<")

to count cells less than 0.
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You are changing the caption
Code:
CCI.Caption = Win & "/" & Loss
    
CCI.Caption = Format(WorksheetFunction.Subtotal(1, Range("L18:L" & LRow)), "$#,0.00")
 
Upvote 0
Thank you Rick...Works Great!... Thank you very much for your help.

First off, here is new code taking into account your specified column (P) and desire to be able to count numbers greater than and also less than zero...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function ZeroCount(Symbol As String)
  Dim Ar As Range
  For Each Ar In Range("P18", Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlVisible).Areas
    ZeroCount = ZeroCount + WorksheetFunction.CountIf(Ar, Symbol & "0")
  Next
End Function[/TD]
[/TR]
</tbody>[/TABLE]
The argument you pass to the function is either "<" to count cells less than 0 or ">" to count cells greater than 0. You can also pass in "=" if you wanted to count the number of zeroes as well. The function assumes the worksheet you want to count is the active worksheet (this is in keeping with the code you posted so far). To use this function in your UserForm, simply assign the function to the Label's Caption property. For example,

Label1.Caption = ZeroCount(">")

to count cells greater than 0; or...

Label2.Caption = ZeroCount("<")

to count cells less than 0.
 
Upvote 0
After you pointed out what was happening, however each time created a new filter requirement, it still doubled the output of the previous results. That being said...I really appreciate all the work you do for this forum. It has been a great value in learning.

You are changing the caption
Code:
CCI.Caption = Win & "/" & Loss
    
CCI.Caption = Format(WorksheetFunction.Subtotal(1, Range("L18:L" & LRow)), "$#,0.00")
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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