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

Case Statement Or Syntax

Status
Not open for further replies.

Eytch

Programmer
Jan 29, 2003
60
0
0
US
Select Case MyValue


Case >= 0.5 0r < 5

Could someone tell me why my Or statement doesn't work?

Thanks,
Eytch
 
I think you want this:
Code:
Case 0.5 To 5

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Could someone tell me why my Or statement doesn't work?"

It does not work because OR is a logic operation.

Or <5 by itself - which is what you have! - is NOT logical.

As PHV has pointed out, the answer to using a Case - in this case (sorry I could not resist) - is 0.5 To 5.

Logically, Or syntax is:

If myValue >= 0.5 Or MyValue < 5

The above logically works.

If myValue > 0.5 Or <5

will fail, and for the same reason.

Or <5 is NOT logical. Because essentially the parser comes back with: "Or WHAT is less than 5???????" It has no idea WHAT is supposed to be less than 5.

Or evaluates expressions. <5 is NOT an expression, it is a value. MyValue <5 is an expression.

Case, on the other hand, DOES (and only does) evaluates values.

So, essentially, you have mixed them up.

Gerry
 
Logically it doesn't work anyway, conditions indicated in the OP will return TRUE for any (numeric) value of MyValue.

The replacement statements mentioned here are only valid if the original idea was

If myValue > 0.5 AND <5
 
Actually, there is no logical condition in the statement, as there is no "OR" but "0r" (ZERO digit & 'r').

combo
 
[rofl]

Good point combo. We shall assume that was a simple typo.

strongm: "conditions indicated in the OP will return TRUE for any (numeric) value of MyValue."

Except.....the OP is not using an IF (which returns a True/False). The OP is using Select Case which functions as an numeric evaluation of MyValue if MyValue is numeric.

Gerry
 
Forgetting the logic of conditions, the proper syntax for "Case >= 0.5 Or < 5" should be (without calling the variable):
Code:
Case Is >= 0.5, Is < 5
i.e. the list of conditions that is equivalent to ORs. If one does not add "Is", they will be automatically completed.

combo
 
Gerry said:
Except.....the OP is not using an IF (which returns a True/False). The OP is using Select Case which functions as an numeric evaluation of MyValue if MyValue is numeric.
I'm not sure that I agree, I'd say that the CASE is evaluated as TRUE if the condition in the CASE statement is met. In the case of the post by strongm, the condition is met (therefore the case is evaluated as TRUE) if the value of MyValue is any number.

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I'm not sure that I agree, I'd say that the CASE is evaluated as TRUE if the condition in the CASE statement is met. In the case of the post by strongm, the condition is met (therefore the case is evaluated as TRUE) if the value of MyValue is any number. "

My bolding.

Huh???? Not in my VBA! Select Case is NOT an IF statement. In my mind, that is precisely the reason and power of Select Case. Select Case does not test if a condition is met. Ever. It evaluates the parameter given to it against...Cases.
Code:
Dim MyValue As Long
MyValue = 113
Select Case MyValue
   Case True
      Debug.Print "yes it is true"
   Case False
      Debug.Print  "no, no bananas"
   Case Else
      Debug.Print "whatever"
End Select
will NEVER be True. OR False. Ever. Select Case will always go to the Case Else. Always.

True is boolean. Select Case will only be able to use:
Code:
Case  True
if the parameter given to it IS boolean.

If the parameter is not boolean (it is declared as a numeric, or uses a numeric value), then using a Case as True will never happen.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Gerry, what about this ?
Code:
Dim MyValue As Long
MyValue = [!]-1[/!]
Select Case MyValue
   Case True
      Debug.Print "yes it is true"
   Case False
      Debug.Print  "no, no bananas"
   Case Else
      Debug.Print "whatever"
End Select
You may also test with MyValue=0 ...
 
Oops, forgot the explanation.
Consider True and False (in VBA) as defined constants with respective value of -1 and 0.
Try this:
If 123 Then Debug.Print "OK"
If 123 = True Then Debug.Print "OK
 
I was just about to amend my previous post.

Yes, that would be a - ahem - True example.

Further...
Code:
Dim MyValue As Long
MyValue = 113

Select Case MyValue
   Case 113
      '  VBA mumbles to itself...[b]True[/b], so...
      '  do the following actions
However PHV, this would only return " Case True" BECAUSE you are using an explicit value of -1. Again, what is being evaluated is the actual numeric value. ANY numeric value, other than -1, will never give a Case True.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Try this:
If 123 Then Debug.Print "OK"
If 123 = True Then Debug.Print "OK"


Ummm, those are IF statements, and IF statements do return True/False.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
How do you explain that the two IF statements doesn't give the same result ?
 
At this point, we are not really talking about IF statements, or at least I am not trying to.

BTW:
If 0 Then Debug.Print "OK"
If 123 = True Then Debug.Print "OK"

DO give the same result. Any other number than 0 will not.

If 5 * 12 Then Debug.Print "OK"

will Print "OK".

So how do I explain that?

Any valid expression (1, -1, 123, 5 * 13, 16.457 / 12) is - because it is valid - ahem....TRUE, therefore the Then is...True. Thus Debug.Print "OK" actions.

While 0 is also valid (and "True") it is, ummm, explicitly 0, which is....False, therefore Debug.Print "OK" will NOT action.

If 123 = True Then Debug.Print "OK"

does NOT action, because while yes 123 is a valid expression in itself, 123 = True is explicitly not. Therefore the IF is not True.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Or put another way...

If 123 Then Debug.Print "OK"
If 123 = True Then Debug.Print "OK"

The IF validates the condition.

123 is valid, and therefore True. Debug.Print "OK"
123 = True is NOT valid, and therefore False.

No Debug.Print "OK"

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Gerry, my point was to explain you that True and False are nothing else than predefined constants.
 
>Select Case is NOT an IF statement. In my mind, that is precisely the reason and power of Select Case. Select Case does not test if a condition is met. Ever. It evaluates the parameter given to it against...Cases.


I'm afraid that you are wrong Gerry.

It evaluates the parameter given to it on the Select Case line.

It then evaluates each case and performs a simple logical comparison (i.e an If) against the already evaluated parameter until the comparison results in True (or we get to the Else statement or end of the Select).

It is, in other words, simply an slightly better structured and often more succint If ... ElseIf ... EndIf. But in the end it still comes down to a simple logical comparison to see if two values are equal.

Select Case x
Case 1
...
Case 2
...
Case Else
...
End Select

is exactly equivalent to

If (x) = (1) Then
...
ElseIf If (x) = (2) Then
...
Else
...
End If

(back in the days of VB5 or perhaps it was VB4 someone decompiled a compile VB program and showed that a Select Case ... End Select and it's equivalent If ... ElseIf ... End If compiled to the same code)

Now, let's consider your apparant counterexample

Dim MyValue As Long
MyValue = 113
Select Case MyValue
Case True
Debug.Print "yes it is true"
Case False
Debug.Print "no, no bananas"
Case Else
Debug.Print "whatever"
End Select

And write it as the exact If statement:

Dim MyValue As Long
MyValue = 113
If (MyValue) = (True) then
Debug.Print "yes it is true"
Elseif (MyValue) = (False) then
Debug.Print "no, no bananas"
Else
Debug.Print "whatever"
End If

It will produce the exact same result (and I believe that it was this point that PHV was trying to get across)

And it is why we can do wacky things like:

Select Case 3 = 7
Case "What" = "Spoon"
MsgBox "oh, ah , erm ..."
Case Else
MsgBox "No, doesn't get here"
End Select
 
I'm afraid that you are wrong Gerry. "

Yes, I suppose that is, ummm, true, but in my defense, I did post:

Case 113
' VBA mumbles to itself...True, so...
' do the following actions

But, yes, I did not really think of it as VBA actually performing an IF statement...but yes...I can see that I am totally wrong, and I will creep away and shut up now.


"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
P.S.

I should know better than to make any contrary comments to anything PHV posts.

Idiot. Me.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top