+ Reply to Thread
Results 1 to 14 of 14

How to incorporate an if then else function to a sum formula

  1. #1
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    How to incorporate an if then else function to a sum formula

    I have the formula
    Please Login or Register  to view this content.
    If zeros appear in any cell in column a I need the corresponding cell in column C to show a zero and the calculation to include the next cell in column A that has a value greater than zero.

    I have attached a sample with a better description.
    Attached Files Attached Files
    Last edited by novice2430; 06-19-2009 at 12:26 PM. Reason: wrong attachment

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

    Re: How to incorporate an if then else function to a sum formula

    I thought this was a good opportunity for me to practice writing custom worksheet functions, so I created one for you called Last3() and installed it into your sample sheet. Here's the code:
    Please Login or Register  to view this content.
    How to install the UDF:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The function works like this, from cell C5 in your sample workbook:
    =LAST3($A$3:B5))

    It's a two-column range at least 3 rows deep. You'll get a "Low Range" error if you put too few rows in the range.

    I nested this into your "make sure column A isn't 0" and got this final formula for C5, copied all the way down the column:

    =IF(A5=0, 0, LAST3($A$3:B5))
    Attached Files Attached Files
    _________________
    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!)

  3. #3
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    Re: How to incorporate an if then else function to a sum formula

    Okay,
    I'll try it out and see if it works for me thanks for your help.

  4. #4
    Forum Contributor kraljb's Avatar
    Join Date
    05-26-2004
    Location
    Illinois
    MS-Off Ver
    2007 (recent change)
    Posts
    256

    Re: How to incorporate an if then else function to a sum formula

    As it's already in VBA, here is a formula that works

    Please Login or Register  to view this content.
    Please note that this is an array formula and needs to be entered with CTRL-SHIFT-ENTER

    I am also uploading up your spreadsheet so you can see the formula (In column D)

    Hope that helps..
    John
    Attached Files Attached Files
    "I don't know what I don't know"

  5. #5
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    Re: How to incorporate an if then else function to a sum formula

    Thanks John.
    I have one concern, my workbook has 10 worksheets so it will be close to 500 arrays working. Will this cause excel to run slow?
    I created a workbook long ago with many arrays and it was a headache.
    Will the same happen by going the other route (VBA)?

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

    Re: How to incorporate an if then else function to a sum formula

    No, the UDF should be much quicker than array formulas. That's why I went that route. I imagined your project was large.

    That is a sweet formula, though, Kraljb.
    Last edited by JBeaucaire; 06-19-2009 at 01:50 PM.

  7. #7
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    Re: How to incorporate an if then else function to a sum formula

    Sorry kraljb, I should have informed all that this is a large project.
    Thanks again JBeaucaire.

  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: How to incorporate an if then else function to a sum formula

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

  9. #9
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    Re: How to incorporate an if then else function to a sum formula

    I have one problem with the UDF. If the first value is a zero, the first calculation includes a zero, and throws things off.
    Attached Files Attached Files

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

    Re: How to incorporate an if then else function to a sum formula

    Here you go, added a test at the end so you only get a value if the COUNTER is 3. With this change, you can now put the formula in C3 and copy down.
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    06-18-2004
    Location
    Canada
    MS-Off Ver
    Office 2016
    Posts
    1,474

    Re: How to incorporate an if then else function to a sum formula

    For what it's worth, here's another formula approach...

    C3, confirmed with CONTROL+SHIFT+ENTER, and copied down:

    =IF(COUNTIF($A$3:A3,">0")>=3,IF(A3=0,0,SUM(IF(ROW($A$3:A3)>=LARGE(IF($A$3:A3,ROW($A$3:A3)),3),IF($A$3:A3,$B$3:B3)))),"")

  12. #12
    Forum Contributor
    Join Date
    06-24-2008
    MS-Off Ver
    2007
    Posts
    139

    Re: How to incorporate an if then else function to a sum formula

    Thanks JBeaucaire I need to learn VBA it seems like simple changes are all I needed.


    Thanks for the array domenic but my worksheet is too large to use arrays.

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

    Re: How to incorporate an if then else function to a sum formula

    There's always something new to learn. I've only just started writing some custom functions, yours was the first I wrote for someone else...or maybe the second.

    Anyway, always always use the forums. I've used them for years and am surprised every week by the new stuff I learn. Whole macros I used to use have been replaced by simple worksheet formulas. I recently wrote a complicated macro that someone else showed a way to do it with about 3 lines of code.

    Hehe, I love it.

    If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].


    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)

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

    Re: How to incorporate an if then else function to a sum formula

    If you have a followup question, be sure to post it here along with a relevant copy of your current workbook.

+ 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