Solved

Countif among filtered data

Posted on 2013-05-13
9
257 Views
Last Modified: 2013-05-15
Hi,

I have a filtered set of data. I want to use
application.countif

Open in new window

on a column to find the number of occurances of a specific data. (how many times a name appears within a filtered month)

However, it is giving me an error.

How can I go on to do this?

Thanks!
0
Comment
Question by:Shanan212
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 23

Assisted Solution

by:NBVC
NBVC earned 150 total points
ID: 39162819
Have you tried like this... assigning to a variable

Var = Application.CountIf(Range("A1:A100"), "Johnny")

Open in new window


or

Msgbox Application.CountIf(Range("A1:A100"), "Johnny")

Open in new window

0
 
LVL 13

Author Comment

by:Shanan212
ID: 39162912
Hi NB,

Yes I did. It returns an error for filtered rows. That is (eg) when only A15, A22, A99 is visible (even though Jonny is on A22)

I only want it to return results from visible cells only.
0
 
LVL 23

Expert Comment

by:NBVC
ID: 39162960
I am more of a formula person... so not sure what best way would be with VBA, but a formula to count a filtered list based on a condition would be something like:

=SUMPRODUCT(SUBTOTAL(3,OFFSET($A$1:$A$100,ROW($A$1:$A$100)-MIN(ROW($A$1:$A$100)),,1)),--($B$1:$B$100="Johnny"))

counts number of "Johnny" in column B when filtering column A.

Not sure if you want to adapt that to VBA and application functions...
0
 
LVL 80

Accepted Solution

by:
byundt earned 250 total points
ID: 39163085
You might consider looping through the visible cells in your range and testing them for the value:
Sub FilteredCellCounter()
Dim cel As Range, rg As Range
Dim i As Long
Set rg = Selection
If rg.Cells.Count = 1 Then Set rg = Intersect(rg.EntireColumn, rg.Worksheet.UsedRange)
Set rg = rg.SpecialCells(xlCellTypeVisible)
If Not rg Is Nothing Then
    For Each cel In rg.Cells
        If cel.Value = "Johnny" Then i = i + 1
    Next
End If
MsgBox i
End Sub

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 100 total points
ID: 39164251
Slightly less complicated than SUMPRODUCT, use:

=SUBTOTAL(#,Range)

The # can be changed to several options from 1 to 11 and 101 to 111.

The help file gives the meaning of each but 2 or 3 and 102 or 103 use the COUNT and COUNTA functions.

Interpreting the help remarks:

1 to 11 include rows hidden by formatting but excludes rows hidden by filter.
101 to 111 excludes rows hidden by formatting and rows hidden by filter.

Thanks,
Rob H
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 39164283
That assumes that the column is already filtered to the value that you are counting.

Thanks
Rob
0
 
LVL 23

Expert Comment

by:NBVC
ID: 39164391
My assumption was that Shanon212 wanted to count only a certain name (e.g. only the number of times "Johnny" appears)  after filtering.  The SUBTOTAL() function on its own will count all rows after filtering.
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 39167281
@NB VC
Hence my second comment!
0
 
LVL 13

Author Closing Comment

by:Shanan212
ID: 39168929
Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now