Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logical Operators (OR)

Status
Not open for further replies.

yumbelie

Programmer
Dec 31, 2002
96
GB
Hi, what is the equivilant of the '|' (Logical OR) operator in Visual Basic? Thing is I thought it was 'Or' but that fails to operate on the code I have:

If ActiveSheet.Cells(crow, 10).Value <> 21 Or 42 Or 37 Then

Response = MsgBox(&quot;Print Me!&quot;, vbExclamation, &quot;Error&quot;)

End If

I've tried enlosing in brackets and the values to test against in quotation &quot;&quot; marks, but still no joy. I reckon it doesn't act on numbers like that, or something. I'm used to C's syntax so this is all still a bit new ;)

Cheers guys


Joseph.
 
Unfortunately, you need to repeat the object for each instance

With Activesheet
If .cells(crow, 10).value <>21 or .cells(crow, 10).value <> 42 or cells(crow, 10).value <> 37 then

However, I would suggest using the select case syntax
Select case Activesheet.cells(crow,10).value
Case 21,37,42
'do stuff
case else
Response = MsgBox(&quot;Print Me!&quot;, vbExclamation, &quot;Error&quot;)
end select

Rgds
Geoff

Si hoc legere scis, nimis eruditionis habes
 
Tuck this little function in your code somewhere:

Function IsOneOf(test, ParamArray list()) As Boolean
Dim i As Integer
For i = LBound(list) To UBound(list)
If test = list(i) Then IsOneOf = True
Next i
End Function

then you can do your test simply as

If not isoneof(ActiveSheet.Cells(crow, 10),21,42,37) Then
...
end if



Rob
[flowerface]
 
Hi,
I find that Select Case is a great tool - good for documenting and clarifying what is happening and can, in some cases, replace If...Then...ElseIF...
Code:
   Select Case ActiveSheet.Cells(crow, 10).Value 
      Case IS <> 21, 42, 37         
         Response = MsgBox(&quot;Print Me!&quot;, vbExclamation, &quot;Error&quot;)
      Case Else
         'else statements
   End Select
Hope this helps :) Skip,
SkipAndMary1017@mindspring.com
 
As a caveat to this - where you have a lot of If ElseIf statements, Select Case can be considerably faster to execute as well. Not sure why - presumably to do with the base code after it has compiled but it does seem to be the case. I rrarely use if / elseif any more as where there is more than just 1 true / false test I use Select Case Rgds
Geoff

Si hoc legere scis, nimis eruditionis habes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top