Consulting

Results 1 to 10 of 10

Thread: Conditional MIN and AVERAGE

  1. #1
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location

    Conditional MIN and AVERAGE

    Happy Friday! Has anyone ever messed around with compiling a VBA project to mimic the Conditional Sum Wizard? I've messed around with the Wizard to try other arguments, such as MAX, MIN, AVG, etc. The only one that seems to work is MAX:

    PHP Code:
    {=MAX(IF($C$2:$C$681>11031,$E$2:$E$681,0))} 
    MIN and AVERAGE don't work, however. I would think they would...oh well.

    Have a great weekend...and THANKS.
    Last edited by Aussiebear; 04-29-2023 at 10:30 PM. Reason: Added code tags

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Not sure about the wizard, but the problem with the formula is the zero if False.

    The Array produced by the IF consists of :
    • Values from the Range $E$2:$E$681 when the check against corresponding cells in the Range $C$2:$C$681 returns TRUE
    • Zeroes when the check returns FALSE

    The MIN of these values (assuming they're positive) will be zero (unless the condition happens to be TRUE for every cell).
    The AVERAGE of these values will be lowered by the inclusion of all the zeroes.

    The SUM and the MAX, on the other hand, are not affected by the extra zeroes in the array.

    If you remove the value-if-false part of the IF function so that you just have ..

    PHP Code:
    {=MAX(IF($C$2:$C$681>11031,$E$2:$E$681))} 
    .. then the array will consist of:
    • Values from the Range $E$2:$E$681 when the check against corresponding cells in the Range $C$2:$C$681 returns TRUE (as before)
    • Boolean FALSE values when the check returns FALSE

    The FALSEs will be ignored by MAX, MIN, and other Functions so that you should then see the results you want (without needing any code).
    Last edited by Aussiebear; 04-29-2023 at 10:30 PM. Reason: Added code tags
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Adding to what Tony's laid out for you, if you want to put Average in there, just replace the Max with it and confirm the formula with Ctrl + Shift + Enter, as it's an array formula (distinguished by 1- the layout, 2- the curly brackets enclosing the formula. If you don't conform w/ CSE, the brackets will disappear).

    Incidently I'm actually working on a userform for just that. It's still in the works but I plan on putting it up on the site for critique. So keep an eye out in the near future.

  4. #4
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    Wow...I hadn't even considered some of those things in Tony's reply. Food for thought on the rest of the Friday.
    Zack...the AVERAGE didn't seem to work, even when I entered it as an array formula. But, I'll keep trying. Thanks!

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Did you put it in like this...

    PHP Code:
    =AVERAGE(IF($C$2:$C$681>11031,$E$2:$E$681)) 
    That's how it needs to be entered, then instead of just enter, do Ctrl + Shift + Enter simultaneously. Should work for ya. You could always go with a non-array'd formula, if you're looking for a formula solution that is.
    Last edited by Aussiebear; 04-29-2023 at 10:31 PM. Reason: Added code tags

  6. #6
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    I did enter as an array formula (Ctrl+Shift+Enter) and it worked. I could swear that I've done that in the past. I've also simply done a FIND & REPLACE throughout the range, which should leave an array formula as is.
    I guess we can consider this one solved. This place is great...thanks!

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Glad you got it working! Glad to help.

    Still keep an eye out for that userform, coming soon to VBAX!

  8. #8
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    Can you divulge any information about it? Sounds like it will be a great tool.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    This is a screenshot of what I've got so far. Only 'bout half way through the coding. I may give the option of using array formula or a non-array formula, but not sure at this point.

    Lemme know what you think....

  10. #10
    VBAX Regular babsc01's Avatar
    Joined
    Jun 2004
    Location
    Southaven, MS
    Posts
    53
    Location
    Very nice...should be great. How about adding COUNT to the available functions? COUNTIF is easy enough to do via formula or status bar, but...as long as you're there.

Posting Permissions

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