Frage

I know I had this working somehow yesterday but end of the day I mistakenly closed the changes without saving. Today I don't know what I'm doing wrong. I have a loop with InputBox and only want to accept 2 potential answers, in this case apple or orange should exit the loop.

    Do
    myValue = InputBox("Enter something")
    Loop While Not myValue = "apple" Or myValue = "orange"

The above will only exit the loop if the first value is entered but not the second. If I add a "Not" for the second value as well like the following, then it doesnt exit the loop regardless of what I type.

    Do
    myValue = InputBox("Enter something")
    Loop While Not myValue = "apple" Or Not myValue = "orange"

I know this is has to be something obvious but I think I've looked at this same code too long to see it. Thanks for any assistance.

War es hilfreich?

Lösung

You probably want an and...

Do
myValue = InputBox("Enter something")
Loop While Not myValue = "apple" And Not myValue = "orange"

Not ands and Not ors are confusing, but if you work it through, you'll figure it out. Example input: apple

NOT "apple" = "apple" is FALSE, and NOT "apple" = "orange" is TRUE. If you use an OR, TRUE or FALSE evaluates to TRUE. Therefore the loop continues. But if you use an AND, the whole statement evaluates to FALSE, which allows an exit.

Andere Tipps

Why not translate English into VBScript:

I want to ask the user until the input is "apple" or "orange"

==>

  Dim sInp
  Do
     sInp = InputBox("Enter something")
     WScript.Echo "+", sInp
  Loop Until sInp = "apple" Or sInp = "orange"
  WScript.Echo sInp

sample output:

+ banana
+ apple
apple
=============

+ pear
+ orange
orange
=============
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top