Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Solved: problem with a conditional average

  1. #1
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location

    Solved: problem with a conditional average

    the following formula work fine when cell H2010 is blank, but as soon as i put a date into the cell, the formula returns 0. cell H2009 also contains a date, and the formula works weather it has a date or is blank. the formula resides in cell H2011.

    =IF(H2010<>"",AVERAGE(IF(AND(J:J>H2009,J:J<H2010),M:M)),AVERAGE(IF(J:J>H200 9,M:M)))

  2. #2
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    You have a gap in 2009 at the end of your formula. It says 200 9.

    <-- edit: n00b
    Last edited by grichey; 06-24-2008 at 08:52 AM.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Gavin,
    That is a common posting problem.
    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'

  4. #4
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    that is an error in the forum. there is no gap in my equation, and there is no space when i try to edit post.

  5. #5
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Your J:J is evaluating as 0. That is why. What are you trying to do?

  6. #6
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    i am trying to average data, that falls between two dates.

    J:J =Date associated with data
    M:M =Data
    H2009 =Date that the data must start after
    H2010 =Data that the data must start before.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    =IF(H2010<>"",
    AVERAGE(IF((J2:J200>H2009)*(J2:J200<H2010),M2:M200)),
    AVERAGE(IF(J2:J200>H2009,M2:M200)))

    this is an array formula, so commit with Ctrl-Shift-Enter, and adjust those ranges to suit, you cannot use whole columns pre-Excel 2007
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Here's a non fancy way to do it and leaves room for error checking. You'll need to change the > and < to >= and =< if you want the dates to be inclusive.

  9. #9
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Quote Originally Posted by xld
    =IF(H2010<>"",
    AVERAGE(IF((J2:J200>H2009)*(J2:J200<H2010),M2:M200)),
    AVERAGE(IF(J2:J200>H2009,M2:M200)))

    this is an array formula, so commit with Ctrl-Shift-Enter, and adjust those ranges to suit, you cannot use whole columns pre-Excel 2007

    Slick. I suck at array formulas and need to get better with them. Any idea why using the evaluate formula on this crashes excel?

    edit: excel 2007 on a better computer (I'm on a P3) did not crash evaluating. So it's either the program or junk PC

  10. #10
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    thanks, i dont know why i didn't think of Multiplying for and.

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by grichey
    Slick. I suck at array formulas and need to get better with them. Any idea why using the evaluate formula on this crashes excel?

    edit: excel 2007 on a better computer (I'm on a P3) did not crash evaluating. So it's either the program or junk PC
    Didn't crash on my 2003 desktop.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    is there an equation to do this were i only need to put the year in H2009 and H2010?

  13. #13
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Quote Originally Posted by figment
    is there an equation to do this were i only need to put the year in H2009 and H2010?
    That's what xld's solution is. He set the range to 200. Just change it to 65000 if you want to do the whole column or 2011 if that's the end of your data set.

  14. #14
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    sorry if i was vague with that last post, but right now i have to put a full date it cells H2009 and H2010, i would like to only put the year part of the Date, so if i want to find the data between the years 2001 and 2003 then i would set:
    H2009 = 2001
    H2010 = 2003
    in stead of the current set up where i have to put:
    H2009 = 12-31-2000
    H2010 = 1-1-2004

  15. #15
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    I havent tried this but this might work:
    =IF(H2010<>"",
    AVERAGE(IF((year(J2:J200)>year(H2009))*(year(J2:J200)<year(H2010)),M2:M200) ),
    AVERAGE(IF(year(J2:J200)>year(H2009),M2:M200)))

  16. #16
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    i tried this and it didn't work.

  17. #17
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    well you can def just insert a column at J and do J1: =year(K1) and fill all the way down and modify accordingly

  18. #18
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    yay i might have to do that, i was hoping not to, for that would significantly increases the size of the file.

  19. #19
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this slight variation of Gavin's formula

    =IF(H2010<>"",
    AVERAGE(IF((YEAR(J2:J200)>=YEAR(H2009))*(YEAR(J2:J20)<=YEAR(H2010)),M2:M200 )),
    AVERAGE(IF(YEAR(J2:J200)>=YEAR(H2009),M2:M200)))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  20. #20
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    i am getting the error "a value used in the formula is of the wrong Data type", now i do have cells in J:J where we dont know the date of a file. these cells are filled with "N/A" to represent this missing data. could this be messing up the equation?

Posting Permissions

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