+ Reply to Thread
Results 1 to 7 of 7

VBA Countif from a named range cell to a specific row

  1. #1
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    VBA Countif from a named range cell to a specific row

    i require a UDF that i can specify a string that identifies a specific named range cell (named MyRefCell)from which to offset by +1 row for start of countif range row/column to another named range row to offset by -1 row as the last cell in the column of the start cell.
    To return true if match > 0 (visible cells only).

    eg. =OffsetVisCountif("MyRefCell","Match this value")

    Refer attached sample workbook.

    MyRefCell is the the name of the reference cell (cell B2) to offset from by 1 row in that column.
    StopHere is the named range of the row (Row 11) to offset by -1 row
    eg. the countif range would be B3:B10
    count of match value ("Match this value") is 3, 2 are visible (visible greater than 0), so return true.

    i hope that makes sense.
    your help would be appreciated
    Attached Files Attached Files

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: VBA Countif from a named range cell to a specific row

    If you're running a version of XL >= 2003 you can actually do this using a formula, albeit a Volatile Sumproduct:

    Please Login or Register  to view this content.
    obviously you may need to tweak this if column B is not a constant

    In UDF terms the following might work ?

    Please Login or Register  to view this content.
    Called from a cell along the lines of:

    =OFFSETVISCOUNT(MyRefCell,StopHere,"Match this Value")

  3. #3
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: VBA Countif from a named range cell to a specific row

    Cheers Donkeyote, that works a treat.
    Out of curiosity, I havent seen the asterisk (*) used before in this fashion. Does this perform the equivalent of AND in this context ?

  4. #4
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: VBA Countif from a named range cell to a specific row

    Perhaps we arent quite there yet after all.
    This appears to only work on the activesheet.
    If i place the function in a formula on a different sheet to that of the data range it does not work.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Countif from a named range cell to a specific row

    Hello Wotadude,

    You just need a simple tweak to Donkey's code...
    Please Login or Register  to view this content.
    Range assumes you are referring to the ActiveSheet. In your case you want to refer to the worksheet of the named range. The Parent property of a range refers to the worksheet on which it resides.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: VBA Countif from a named range cell to a specific row

    Thanks Leith.
    I must remember use of the Range Parent reference.
    However i found that this still produced an error (Application-defined or object-defined error).
    So modified it to -
    Please Login or Register  to view this content.
    So i assume this should now work as needed.
    Out of curiosity, I havent seen the asterisk (*) used before in this fashion. Does this perform the equivalent of AND in this context ?
    Last edited by wotadude; 07-20-2009 at 05:14 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Countif from a named range cell to a specific row

    Hello Wotadude,

    Yes, it is like using the boolean "And" operator. Each expression in parentheses evaluates to either a True or False. In VBA True and False have the numerical equivalents of -1 and 0 respectively. Mathematically multiplying any number by zero results in zero or in Boolean algebra - False.

+ 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