+ Reply to Thread
Results 1 to 9 of 9

COUNTIF and named ranges

  1. #1
    Registered User
    Join Date
    11-06-2009
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Talking COUNTIF and named ranges

    Greetings!

    I think I've discovered a limitation of COUNTIF but I need confirmation from the experts on this board as well as any ideas for a workaround. In a nutshell, I'm trying to use COUNTIF with a named range that consists of non-contiguous cells.

    For example, I have the following named range called P1Q1 defined in Sheet1:

    =Sheet1!$B$2,Sheet1!$G$2,Sheet1!$L$2,Sheet1!$Q$2,Sheet1!$V$2,Sheet1!$AA$2,Sheet1!$AF$2,Sheet1!$AK$2

    As you can see this range is comprised of cells that are located in a single row but multiple, separated columns. I want to use the COUNTIF function to analyze that range and count the number of times an "X" appears. Here is the function I'm using:

    =COUNTIF(P1Q1,"X")

    I'm receiving this error:

    "The value used in the formula is of the wrong data type."

    I've seen dozens of examples out there that use a named range as the range argument in a COUNTIF function but in all of those examples the named range was a group of contiguous cells. I would not expect Excel to have trouble with this but it clearly does.

    Anyone have any ideas for a workaround on this? Thanks in advance!

  2. #2
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: COUNTIF and named ranges

    It doesn't use your named range, but it seems to work: =SUMPRODUCT(--(B2:AK2="X"),--(MOD((COLUMN(B2:AK2)-2),5)=0))
    It ignores cells not currently in your named range, like C2.

  3. #3
    Registered User
    Join Date
    11-06-2009
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: COUNTIF and named ranges

    dark...

    Thanks so much for your reply, that works great!

    I forgot to mention this in my original post but say I want to check for two different string values "X" or "G"? How would I modify the formula to achieve that? I'll play around with it on my end but any assistance you can provide would be much appreciated.

  4. #4
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: COUNTIF and named ranges

    Replace "X" with {"X","G"}

  5. #5
    Registered User
    Join Date
    11-06-2009
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: COUNTIF and named ranges

    Is this how the final formula should read:

    =SUMPRODUCT(--(B2:AK2="X","G"),--(MOD((COLUMN(B2:AK2)-2),5)=0))

    If so, that doesn't work. I get the generic "Your formula contains an error."

  6. #6
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: COUNTIF and named ranges

    Nope, the curly brackets makes what's in them an array. It should read like =SUMPRODUCT(--(B2:AK2={"X","G"}),--(MOD((COLUMN(B2:AK2)-2),5)=0))

  7. #7
    Registered User
    Join Date
    11-06-2009
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: COUNTIF and named ranges

    Sorry dark...that's not working either. Here's the formula I'm using copied verbatim from my sheet:

    =SUMPRODUCT(--(B2:AK2={"X","G"}),--(MOD((COLUMN(B2:AK2)-2),5)=0))

    I get the error:

    "A value is not available to the formula or function"

  8. #8
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: COUNTIF and named ranges

    Odd. I've seen that used so often before here in the forum. Anyway, I tested this version before posting it. =SUMPRODUCT((B2:AK2="X")+(B2:AK2="G"),--(MOD((COLUMN(B2:AK2)-2),5)=0))
    Sorry for the error.

  9. #9
    Registered User
    Join Date
    11-06-2009
    Location
    Pennsylvania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: COUNTIF and named ranges

    Now we're in business! Thanks again!!

+ 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