+ Reply to Thread
Results 1 to 10 of 10

Or statement tested first

  1. #1
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387

    Or statement tested first

    I'm wondering if there's a way to have one part of an Or statement tested first.

    For example, I have a statement that if it's not true, then the second part of my test will cause an error, so I need the first statement to always be tested before the second.

    I could write this statement with a nested if, but my coding would look prettier with an Or. Any ideas?

  2. #2
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Or statement tested first

    Can you give an example of the data?

  3. #3
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387

    Re: Or statement tested first

    My example is pretty complex, so I'll make one up.

    Please Login or Register  to view this content.
    I'm going to want to evaluate divider = 0 first, so that if it does equal zero I don't attempt to divide a number by 0.

  4. #4
    Valued Forum Contributor squiggler47's Avatar
    Join Date
    02-17-2009
    Location
    Littleborough, UK
    MS-Off Ver
    Excel 3.0 to 2007+2010! (couldnt get 2.1 working)
    Posts
    1,013

    Re: Or statement tested first

    Its down to the IF, even with AND() it evaluates both halfs instead of failing after the first!

    =10/(ISERROR(10/C11)+C11)=5

    =IF(C11=0,FALSE,10/C11=5)

    =10/(C14-(C14=0))=5

    are other options
    Last edited by squiggler47; 03-10-2009 at 11:21 AM.

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Or statement tested first

    Why not go:

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387

    Re: Or statement tested first

    Quote Originally Posted by davegugg View Post
    Why not go:

    Please Login or Register  to view this content.
    That's what I currently have. It works correctly, but it makes the code a lot messier, and when you have over 100 pages of vb in one excel file, you tend to look for the simplest way.

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

    Re: Or statement tested first

    Testing the divisor for zero first with an IF query IS the easiest way.
    _________________
    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!)

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Or statement tested first

    Could you maybe go with a select case?

    Please Login or Register  to view this content.
    I don't know as much about Select Case, is it faster/neater?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

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

    Re: Or statement tested first

    The fastest is IF, get's the job done every time in the first step. Anything else is really just having fun with VBA.

  10. #10
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Or statement tested first

    but my coding would look prettier with an Or
    You're in the wrong job

    but seriously - it's not just unnecessary - it's harder to debug later - I know people who insist on using IF blocks for single option scenarios - like:
    if a then
    do b
    end if

    because it's easier to read

    Making vba shorter does not make it better - commenting properly makes it better
    !!

    CC

+ 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