using "or" with strings

G

Guest

I would like to use an if... then statement as follows

if string1 = string2 or string3 then

I get a type mismatch error though, since, I guess, "or" only works with
numerical values. How can I compare the string to more than one value?
 
S

Stuart McCall

Wavequation said:
I would like to use an if... then statement as follows

if string1 = string2 or string3 then

I get a type mismatch error though, since, I guess, "or" only works with
numerical values. How can I compare the string to more than one value?

if string1 = string2 or string1 = string3 then
 
J

John Nurick

I would like to use an if... then statement as follows

if string1 = string2 or string3 then

I get a type mismatch error though, since, I guess, "or" only works with
numerical values. How can I compare the string to more than one value?

If you think you can use
If Number1 = Number2 or Number3 Then
you probably don't understand what you are doing. Try this in the
Immediate pane:

If 1 = 2 Or 3 Then Debug.Print "Someone has blundered!"
 
J

John W. Vinson

I would like to use an if... then statement as follows

if string1 = string2 or string3 then

I get a type mismatch error though, since, I guess, "or" only works with
numerical values. How can I compare the string to more than one value?

As Stuart and John say, you're misusing the OR operator here. It *looks* like
the English language conjunction, but it isn't! It's a logical operator, just
like the < less-than or the + addition operator; and it has a very strict
definition: it takes two arguments, the expression to its left and the
expression to its right.

<expr1> OR <expr2>

is TRUE if either expr1 is true, or expr2 is true; it is FALSE if neither is
true.

Since a string isn't a logical expression, your statement gets a type mismatch
- you're comparing a logical expression (string1 = string2) with a String. If
you're using numbers, then

1 = 2 OR 3

will compare two logical expressions:

1=2 (which is FALSE)

and

3 (which is TRUE, since any nonzero value is true)

and will give TRUE as a result.

Bottom line: you need to use two logical expressions as the arguments to OR:

string1 = string2 ... an expression which is either true or false
string1 = string3 ... another such
string1 = string2 OR string1 = string3 ... will be true if either of the
above is true.


John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top