+ Reply to Thread
Results 1 to 11 of 11

CountIF - combine results from two columns

  1. #1
    Registered User
    Join Date
    07-02-2007
    Posts
    42

    CountIF - combine results from two columns

    Hi,

    I have two columns A and B. Column A contains 'Machine Nr' and column B contain 'Week Nr'. In cell C1 i want to write a function so that it counts the number of weeks in Column B where week number is 4 AND the machine nr is 1 in column nr A. Can anyone help me here plzzzz.

    Column A Column B
    Machine Nr Week Nr
    1 4
    1 4
    2 4
    2 4
    3 4
    3 4
    Last edited by VBA Noob; 02-15-2009 at 04:35 PM.

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

    Re: CountIF - combine results from two columns

    Perhaps:

    =SUMPRODUCT(--($A$1:$A$100=$C$1),--($B$1:$B$100=$D$1))

    where C1 is cell containing Machine No of interest (1) & D1 is cell containing week no of interest (4)

    If you're using XL2007 you could use COUNTIFS as opposed to SUMPRODUCT.

  3. #3
    Registered User
    Join Date
    07-02-2007
    Posts
    42

    Re: CountIF - combine results from two columns

    the SUMPRODUCT multiplies the two arrays but i am not interested in multiplying the arrays but want to COUNT the nr of weeks where machine nr is 1

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

    Re: CountIF - combine results from two columns

    Please have the decency to test the solution provided before dismissing.

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: CountIF - combine results from two columns

    Hello tahirawan11,

    Although SUMPRODUCT does, as you say, multiply the 2 arrays, that will, in this case result in the count you need.

    Did you try DonkeyOte's suggestion?

  6. #6
    Registered User
    Join Date
    07-02-2007
    Posts
    42

    Re: CountIF - combine results from two columns

    Hi all,

    Thanks for all your posts, yes i did tried the DonkeyOte's suggestion before replying but the solution did not worked for me. Please see the attachments.

    i also moved the column A and B into C and D and tried the formula but with no luck. i dont know what i am doing wrong.

    /tahirawan11
    Attached Files Attached Files

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

    Re: CountIF - combine results from two columns

    I'm not quite sure what you're trying to achieve but based on your sample you should use:

    C2: =SUMPRODUCT(--($A$2:$A$2000=$A2),--($B$2:$B$2000=$B2))
    copy down

    Without knowing as to the purpose of the sheet in question it's hard to comment further but given volume of calcs being performed there is invariably a better approach but we would need to understand how these results are to be used.

    EDIT:

    For ex C could create a concatenation

    C2: =$A2&"~"&B2
    copied down

    Then you can avoid array and use COUNTIF

    D2: =COUNTIF($C$2:$C$2000,$A2&"~"&$B2)
    copied down

    More efficient than Sumproduct.
    Last edited by DonkeyOte; 02-15-2009 at 06:23 PM.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: CountIF - combine results from two columns

    You didn't adjust the formula in any way for your own data?

    In C2:
    =SUMPRODUCT(--($A$1:$A$3100=A2),--($B$1:$B$3100=4))

    You have to expand that range to cover your data, and make the EQUAL comparisons match. You were still using C1 and D1 and there was nothing in those cells to match.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    07-02-2007
    Posts
    42

    Re: CountIF - combine results from two columns

    Thanks a lot DonkeyOte. Your solution has solved my problem.

    May be i didnt made my self clear enough. I wanted to know how many times each Machine Nr was used in each week and now i can see it in column C, for eg Machine 1 was used 35 times in week 49 and 10 times in week 50 and so on

    Many thanks

  10. #10
    Registered User
    Join Date
    02-17-2009
    Location
    Brasil
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: CountIF - combine results from two columns

    Hello!

    This almost solved my problem. I'm using this formula:

    =SUMPRODUCT(('Commerce'!$H$1:$H$1000=$B5)*('Commerce'!$J$1:$J$1000=D$4))

    But the thing is, it's comparing *exactly* with the B5 and D4 cells. Actually I want to make a lookup in the cell to see if there's the word in it, like:

    =COUNTIF('Commerce'!$H:$H;"*" & B9 & "*")

    Do you know if it's possible?

    Thanks,
    efilipe.

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

    Re: CountIF - combine results from two columns

    efilipe, please do not hijack another's thread, you should create your own.

+ 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