+ Reply to Thread
Results 1 to 5 of 5

simplfying an IF statement

  1. #1
    Registered User
    Join Date
    06-06-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    91

    simplfying an IF statement

    Hi all,

    Placed in VBA as I'm coding it in VBA, though technically this question could probably be applicable to any if statement in any language.

    I currently have an if statement that goes like:

    Please Login or Register  to view this content.
    I've always wondered if there's a way to not have the statement have to technically evaluate twice, I've not actually seen anything around google (or don't know what to search for).
    If the value is not 0, use that already calculated value but if it's zero print "zero".

    Essentially I'm looking for

    Please Login or Register  to view this content.

    It gets pretty bad if for example I have nested if statements or a row of if statements all doing the same thing and the code becomes bloated with what appears to be redundancy.

    Thanks.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: simplfying an IF statement

    Sometimes there is no other way around it. Other times you could use a helper column to run the "long formula", then test the helper...
    =if(helper<>0,helper,0)

    essentially the same thing, but the reference would be far shorter
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: simplfying an IF statement

    Calculate the result of the long evaluation formula and test the result for zero.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    06-06-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    91

    Re: simplfying an IF statement

    Much appreciated.

    I'll mark this is a solved.

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: simplfying an IF statement

    Happy to help and thanks for the feedback

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] IF Statement with Multi-Criteria Statement inside a Data Validation List
    By dharvey1978 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-31-2020, 09:10 PM
  2. compile error expected line number statement end statement
    By mattress58 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2014, 10:12 AM
  3. VBA Compile Error : line number or label or statement or end of statement
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-04-2013, 06:41 PM
  4. Replies: 4
    Last Post: 06-01-2012, 10:05 AM
  5. Replies: 4
    Last Post: 05-16-2012, 05:33 PM
  6. [SOLVED] Utilize a Select Case Statement in Target Intersect Statement
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2009, 08:55 PM
  7. Simplfying an "if" formula
    By Karman in forum Excel General
    Replies: 3
    Last Post: 02-01-2008, 03:27 PM

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