+ Reply to Thread
Results 1 to 5 of 5

Number of rows where two column cells have same value ?

  1. #1
    Registered User
    Join Date
    08-08-2007
    Posts
    6

    Number of rows where two column cells have same value ?

    Hello

    I've created the below vba code in Excel which returns the total number of rows where column D ="Y". What I would like to do is to find out the total number of rows where column D = "Y" and column E = "Y".


    Code:

    For Each cell In ActiveWorkbook.Sheets("Survey").Range("D1:D300")

    If cell.Value = "Y" Then
    CountD = CountD + 1
    End If
    Next cell

    ThisWorkbook.Sheets("Results").Cells(7, 2) = CountD


    All help is greatly appreciated

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996
    Is there a reason you can't just use a formula?

    This formula returns the count of rows where Col_D and Col_E contain "Y"

    =SUMPRODUCT((D1:D20="Y")*(E1:E20="Y"))

    Or do you need to do that with VBA?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    08-08-2007
    Posts
    6
    Thanks for the formula. Do you know whats wrong with the following formula as I'm getting an error.

    =(SUMPRODUCT((Survey!D1:D200="Y")*(Survey!F1:F200="Y"))) / SUMPRODUCT(Survey!D1:D200="Y")

  4. #4
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Number of rows where two column cells have same value ?

    Yes.....Here's the reason...

    A boolean expression (fancy phrase for an expression that returns a True/False value) does NOT return a numeric result.

    Consequently, this:
    Survey!D1:D200="Y"

    returns a series of True and False values.
    SUMPRODUCT is trying to add them together...and failing.

    However, you can coerce those values into proper numbers by forcing Excel to implicitly convert True to 1 and False to 0 by applying a mathematical operator to them.

    The generally accepted approach by knowledgable users is this:
    --(Survey!D1:D200="Y")

    The first minus sign (-) makes True = -1 and False = 0.
    The second minus sign converts those values to 1 and 0, respectively.

    You could also multiply the values by 1, but that might mislead users. Whenever you see the double-minus sign in a formula, that's an indication that it's only purpose is to force a conversion to numbers.

    So, you're formula could be:
    Please Login or Register  to view this content.
    BUT, it should be this:
    Please Login or Register  to view this content.
    Note: there are NO spaces in those formulas

    Does that help?

  5. #5
    Registered User
    Join Date
    08-08-2007
    Posts
    6
    Thanks, it works perfectly

+ 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