ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If OR Statement... (https://www.excelbanter.com/excel-programming/335668-if-statement.html)

Thomas

If OR Statement...
 
I'm still new at this VB stuff, and did not know how to do An Or in the If
Statement.

IF this OR that Then
blahblahblah...
End IF

Thanks for your help whoever you may be,
Thomas :)

JE McGimpsey

If OR Statement...
 
You've got it right:

Public Function foo(this As Boolean, that As Boolean) As String
If this Or that Then
foo = "blahblahblah..."
End If
End Function


In article ,
thomas wrote:

IF this OR that Then
blahblahblah...
End IF


Tom Ogilvy

If OR Statement...
 
Just to add, if you were testing a range equal to either of two values as an
example

If Range("A1") 10 or Range("A1") < 1 then
msgbox "Out of bounds
End If

You can't do something like

if Range(A1) 10 or < 1 then

Each side of the OR must a an expression that can be evaluated to or coerced
to a boolean value.
--
Regards,
Tom Ogilvy

"thomas" wrote in message
...
I'm still new at this VB stuff, and did not know how to do An Or in the If
Statement.

IF this OR that Then
blahblahblah...
End IF

Thanks for your help whoever you may be,
Thomas :)




scattered[_2_]

If OR Statement...
 


thomas wrote:
I'm still new at this VB stuff, and did not know how to do An Or in the If
Statement.

IF this OR that Then
blahblahblah...
End IF

Thanks for your help whoever you may be,
Thomas :)


just use Or between boolean statements:

If A = B Or A = C Then
msgbox "A is B or C"
End If

The only thing to watch out for is that, unlike in say C, VBA's "Or" is
not evaluated via short-circuit evaluation. This means that in

P Or Q

Q is evaluated even if P is true. Thus

If B = 0 Or 1/B 5 Then ...

throws a run-time division by zero error if B happens to be zero, so
you would be forced into a more verbose way, perhaps involving
if-then-else or nested ifs for that sort of thing. VBA's "And" has the
same lack of short-circuit evaluation, which is one of the more
annoying things about VBA

-John Coleman



All times are GMT +1. The time now is 04:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com