Consulting

Results 1 to 10 of 10

Thread: Conditional count (two conditions)

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location

    Conditional count (two conditions)

    Hi,

    I have a range consisting of two columns, one with codes and one with dates. (That is, to each code (shown in the first column) you have a date attached (shown in the column to the right of the code). For each code I want to count the number of times this code is shown, grouped by date intervals, that is, for ex how many times the code A2B is shown within the time interval 05/01/2006 to 05/31/2006.

    Thus, the result should be a table showing how many times A2B occured during january, how many times the code A2B occured during February and so on for each month and for each code.

    Also this range is dynmamic, ie people can add and/or remove codes from the range.

    All ideas are welcome!

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hi danlu, welcome to VBAX

    Would it be possible to post your workbook? Or a dummy workbook (one without personal/sensitive info)?

    I think I understand what you mean, but it would be much easier with a visual example




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Assuming that you have headings, use a dynamic named range of say Codes

    =OFFSET($A$1,1,,COUNTA($A:$A)-1)
    and another of say Dates

    =OFFSET($A$1,1,1,COUNTA($A:$A)-1)
    and then use

    =SUMPRODUCT(--(Codes="A2B"),--(ISNUMBER(Dates)),--(MONTH(Dates)=1))

  4. #4
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    See attached doc.

    On tab "table" you see as an example the frequency for two codes for a certain month, that is, how often these two codes occur during May. The final table should of course include all codes and all months for each code.

    On tab "rangeofvalues" you have my data.

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Using pretty much what xld said, I used:

    =SUMPRODUCT(--(Codes=B$1),--(MONTH(Dates)=ROWS($2:2)))

    And copied down (from January's row).

    Check out the example, be sure to look at the defined named ranges (Insert-->Name-->Define...)




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You need to setup the names as I described, then you need to change all the dates strings to real dates, and then use

    =SUMPRODUCT(--(Codes=B$1),--(TEXT(Dates,"mmmm")=$A6))

  7. #7
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    Hi,

    I tend to get the #Value error, which I also get when I open my attached sheet where you have added the sumproduct formulas. What could be the reason (the dates are in real date format), could it have anything to do with the formatting of the formulas? What means "--" as you use in the sumproduct formula?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's a sample using XLD's code
    -- forces a change from True, False values to 1 and 0
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    Hi,

    Must be something with my settings, because when I look in your sheet I have the #NAME? problem both in the dates column and in the table where each code per month should be summed.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sorry,
    My fault,
    I used the RandBetween function to make up some dates and this needs the Analysis Toolpack installed.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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