Consulting

Results 1 to 7 of 7

Thread: Solved: udf: countif for multiple ranges and sigle or multiple criteria

  1. #1
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644

    Solved: udf: countif for multiple ranges and sigle or multiple criteria

    hi board.

    i need a udf for counting values in non-contiguous ranges.

    single_crit_value = CountIf(Range1, CritRange1) + CountIf(Range2, CritRange1) + ... + (RangeN, Crit1)

    or

    multi_crit_value = CountIf(Range1, CritRange1) + CountIf(Range1, CritRange2) + ... + (RangeN, CritRange7)

    is that possible?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why do you need a UDF, what is wrong with the formula?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    thanks for replying.

    formula is OK. i want to use one formula instead of formula1 + formula2 etc.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you give an actual example then, it will probably affect the formula given.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    thx again xld.

    attached is a representative sample of my cese.

    i adopted a udf from ozgrid which counts the values which are "equal" to criteria in 2-5 ranges.

    the problem is with comparison operators, since the logic is result = result+1.
    Attached Files Attached Files

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would use this modified UDF

    [vba]

    Function CountIf_MR(crit, ParamArray rng())
    Dim vResult As Long

    For i = LBound(rng) To UBound(rng)

    vResult = vResult + Application.CountIf(rng(i), crit)
    Next i

    CountIf_MR = vResult

    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644

    thanks for your time.

    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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