SeekingAlpha
SeekingAlpha

Reputation: 7797

Infinite while loop in VBA

Can anyone see whats wrong with my while loop below? Basically I want to request a user to enter either "m" or "w", however, when I run it seems to enter an infinite loop despite the user entering "m" or "w".

While period <> "m" Or period <> "w"
    period = InputBox(Prompt:="Please enter the period (m/w): ", Title:="Period")
Wend

Upvotes: 0

Views: 1075

Answers (2)

Daniel
Daniel

Reputation: 13122

It is an infinite loop because if period = "m" then period <> "w" and vice versa

Switching to the following is likely what you want.

While period <> "m" AND period <> "w"
    period = InputBox(Prompt:="Please enter the period (m/w): ", Title:="Period")
Wend

Upvotes: 3

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

I think you mean And instead of Or pretty much any input will not be 'w' or 'm' including w and m. (w is not equal to m so that conditional is still true and vice versa).

Upvotes: 2

Related Questions