+ Reply to Thread
Results 1 to 5 of 5

COUNTIF criteria in VBA

  1. #1
    Registered User
    Join Date
    05-17-2006
    Posts
    7

    COUNTIF criteria in VBA

    What vba function can I use that checks criteria much like SUMIF or COUNTIF uses? In other words, say you want to write COUNTIF that only includes visible cells...

    Public Function CountVIf(rng As range, criteria As String)
    Dim cell As range, cmd As String

    For Each cell In rng
    If cell.RowHeight <> 0 And cell.ColumnWidth <> 0 Then
    cmd = "COUNTIF(" & cell.Address & ",""" & criteria & """)"
    CountVIf = CountVIf + Evaluate(cmd)
    End If
    Next cell
    End Function

    How can I do this without having to rely on Evaluate("COUNTIF...."?

    Thanks!

  2. #2
    Ardus Petus
    Guest

    Re: COUNTIF criteria in VBA

    Public Function CountVIf(rng As Range, criteria As String) As Long
    Dim cell As Range, cmd As String

    For Each cell In rng
    If cell.RowHeight <> 0 And cell.ColumnWidth <> 0 Then
    cmd = "=" & cell.Address & criteria
    If Evaluate(cmd) Then CountVIf = CountVIf + 1
    End If
    Next cell
    End Function

    HTH
    --
    AP

    "whitehurst" <[email protected]> a
    écrit dans le message de news:
    [email protected]...
    >
    > What vba function can I use that checks criteria much like SUMIF or
    > COUNTIF uses? In other words, say you want to write COUNTIF that only
    > includes visible cells...
    >
    > Public Function CountVIf(rng As range, criteria As String)
    > Dim cell As range, cmd As String
    >
    > For Each cell In rng
    > If cell.RowHeight <> 0 And cell.ColumnWidth <> 0 Then
    > cmd = "COUNTIF(" & cell.Address & ",""" & criteria & """)"
    > CountVIf = CountVIf + Evaluate(cmd)
    > End If
    > Next cell
    > End Function
    >
    > How can I do this without having to rely on Evaluate("COUNTIF...."?
    >
    > Thanks!
    >
    >
    > --
    > whitehurst
    > ------------------------------------------------------------------------
    > whitehurst's Profile:
    > http://www.excelforum.com/member.php...o&userid=34544
    > View this thread: http://www.excelforum.com/showthread...hreadid=550351
    >
    >




  3. #3
    Registered User
    Join Date
    05-17-2006
    Posts
    7
    Thank you, I figured criteria expressions could be more complicated than just appending, but I guess that can't be!

    Can anybody think of a way to write CountVIf without having to loop over each cell? I was wanting to use SpecialCells(xlCellTypeVisible) and then just pass the result range (which would have multiple areas) into EVALUATE(COUNTIF(newRange, criteria)), but specialcells doesn't seem to work in UDFs - which sucks.

    Thanks.

  4. #4
    Ardus Petus
    Guest

    Re: COUNTIF criteria in VBA

    You have to loop thru independent cells, because you must evaluate condition
    against each cell's contents.

    --
    AP

    "whitehurst" <[email protected]> a
    écrit dans le message de news:
    [email protected]...
    >
    > Thank you, I figured criteria expressions could be more complicated than
    > just appending, but I guess that can't be!
    >
    > Can anybody think of a way to write CountVIf without having to loop
    > over each cell? I was wanting to use SpecialCells(xlCellTypeVisible)
    > and then just pass the result range (which would have multiple areas)
    > into EVALUATE(COUNTIF(newRange, criteria)), but specialcells doesn't
    > seem to work in UDFs - which sucks.
    >
    > Thanks.
    >
    >
    > --
    > whitehurst
    > ------------------------------------------------------------------------
    > whitehurst's Profile:
    > http://www.excelforum.com/member.php...o&userid=34544
    > View this thread: http://www.excelforum.com/showthread...hreadid=550351
    >




  5. #5
    Gary''s Student
    Guest

    Re: COUNTIF criteria in VBA

    The problem is not with SpecialCells, it is with COUNTIF(). Even on the
    worksheet COUNTIF won't work on dis-joint ranges. That's why the
    cell-by-cell approach.

    In:
    http://www.microsoft.com/communities...a-e20c1dc1c7ba

    Bob Phillips gave a very clever solution to this.


    --
    Gary''s Student


    "whitehurst" wrote:

    >
    > Thank you, I figured criteria expressions could be more complicated than
    > just appending, but I guess that can't be!
    >
    > Can anybody think of a way to write CountVIf without having to loop
    > over each cell? I was wanting to use SpecialCells(xlCellTypeVisible)
    > and then just pass the result range (which would have multiple areas)
    > into EVALUATE(COUNTIF(newRange, criteria)), but specialcells doesn't
    > seem to work in UDFs - which sucks.
    >
    > Thanks.
    >
    >
    > --
    > whitehurst
    > ------------------------------------------------------------------------
    > whitehurst's Profile: http://www.excelforum.com/member.php...o&userid=34544
    > View this thread: http://www.excelforum.com/showthread...hreadid=550351
    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1