Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Excel VBA If OR statements


Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2010, 01:28 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default Excel VBA If OR statements


When you write a statement like:

If x = 1 or x =2 or x =3 then
else
endif

Does VBA check the first condition and if x = 1, it doesnt check the other two conditions? Or does it still check the other two?
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #2  
Old 06-25-2010, 02:13 PM
Visvang's Avatar
Visvang Visvang is offline
Centurion
 
Join Date: Mar 2010
Posts: 110
Default

as far as i can tell there is no way to tell as vba looks as a whole line at time;
why would you want to know this???
__________________
Whether you believe you can do a thing or not, you are right - Henry Ford

Record a macro if your not sure how things work!
Reply With Quote
  #3  
Old 06-25-2010, 02:29 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

I assume that VBA behaves the same way as VB6 - ALL of the conditions are evaluated and then those logical results are ORed together.

This is different from how some C compilers would perform the test where they would only evaluate each condition left to right until a TRUE result was found.
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
  #4  
Old 06-25-2010, 02:31 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

Okay thanks, I only ask because if a second logical errors out then I am thinking if it reads the whole line, the whole line will error out and that you cannot put the first logical code that doesnt error at the front to avoid it from trying to run the second OR. Just trying to save myself from adding an extra two lines of code.
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #5  
Old 06-25-2010, 03:07 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

Correct you are - as an example to those reading this later on:

Code:
Dim bValue as Boolean Dim i as Integer Dim lArray(10) as Long Dim lptr as Long bValue = False i = 42 lptr = 11 If bValue OR i > 5 OR lArray(lptr) = 1492 Then 'do something EndIf
The line with the IF statement will throw an error due to the attempt to access a non-existent element of lArray.
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
  #6  
Old 06-25-2010, 07:52 PM
Josh Hazel Josh Hazel is offline
Senior Contributor
 
Join Date: May 2008
Posts: 805
Default

Think you meant to set bValue = True, but yeah even so ... i see it does still throw the error. So I guess, just trial and error. It seems like a waste for VBA to read the entire line and not just step through the OR and stop once it finds a true value.
__________________
Josh

If Google = NoHelp Then PostHere = True
Reply With Quote
  #7  
Old 06-26-2010, 01:49 PM
Colin Legg's Avatar
Colin Legg Colin Legg is offline
Out Of Office

Retired Moderator
* Expert *
 
Join Date: Mar 2005
Location: London, UK
Posts: 3,398
Default

You're right, Josh: short-circuiting would be more efficient. Of course the same idea applies for And - if a False condition is met then what's the point in evaluating the rest of the line?

In VB .Net OrElse and AndAlso have been introduced which do exactly that. For a while I wondered why these new keywords were necessary - why not just introduce the short-circuiting to Or and And?

One situation that has been suggested would be where you have functions being called on the line, with each function doing something and then returning True or False. ie.
Code:
If DoSomething1 Or DoSomething2 Or DoSomething3 Then
It might be that the coder expects (or requires) all three functions to do their stuff. Short-circuiting would mean that didn't happen and therefore there might be issues with VB6 --> VB.Net conversions , so the new keywords for short-circuiting were required. I'm not saying I necessarily agree with that way of coding in the first place, just that this is one explanation I've been given.
__________________
RAD Excel Blog
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->