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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does this IF OR code not work?

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
if lTheDate = 0 Or InStr(rTemp1.Cells(1, 1).Value, "Strange") = 0 Or InStr(rTemp1.Cells(1, 1).Value, "Special") = 0
then
msgbox "no matches"
else
msgbox "found a match"

I've checked and InStr(rTemp1.Cells(1, 1).Value, "Special") does indeed = 1, but the else part of this if condition doesn't get executed.

Any ideas?

Thansk,

Chris
 
InStr(rTemp1.Cells(1, 1).Value, "Special") does indeed = 1

Then you wouldn't hit the else

maybee
InStr(rTemp1.Cells(1, 1).Value, "Special") <> 0

As your testing against a string I'd switch to
Code:
If lTheDate <> 0 Or InStr(rTemp1.Cells(1, 1).[COLOR=red]Text[/color], "Strange") <> 0 Or InStr(rTemp1.Cells(1, 1).Text, "Special") <> 0 then
    msgbox "no matches"
else
    msgbox "found a match"
End If

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Delete Delete, where's that delete button
Code:
If lTheDate = 0 Or InStr(rTemp1.Cells(1, 1).Text, "Strange") = 0 Or InStr(rTemp1.Cells(1, 1).Text, "Special") = 0 then
    msgbox "no matches"
else
    msgbox "found a match"
End If



[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 



Don't you want to say

Code:
If NoMatchCondition1 [b]AND[/b] NoMatchCondition2 [b]AND[/b] NoMatchCondition3 Then
  NO Match
Else
  Got at least one match
End if

Skip,
[sub]
[glasses] [b][red][/red][/b]
[b][/b] [tongue][/sub]
 
It used to work before I put the third OR in...


I was wondering why the third OR made it stop working?
 
It used to work before I put the third OR in...
If lTheDate = 0 Or [!]([/!]InStr(rTemp1.Cells(1, 1).Value, "Strange") = 0 [!]And[/!] InStr(rTemp1.Cells(1, 1).Value, "Special") = 0[!])[/!] Then


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV,

How does that logic work? If 1 of the conditions in the red brackets goes is false I'd like it to go into the else condition... Doesn't both have to be false for it to go into the else conidition?

Thanks a lot,

Chris
 
Hi
I don't want to tread on toes here but the logic is that both expressions in the brackets need to evaluate to true in order for the bracketed statement to evaluate to true. If either (or both) of the expressions evaluate to false then the whole bracketed expression is false.

ie if "Special" or "Strange" are found then the bracketed expression is false. If both "Special" & "Strange" are found then the bracketed expression is false. If nether are found then the bracketed expression is true.

I just needed to write that down to see if I understood myself!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top