subha
subha

Reputation: 65

VBA do until loop conditioning?

I have to use 'DO UNTIL' condition if certain conditions are met i will give an

example here.

For the cells(9,3) in an sheet, i have to check whether the cells(9,3).value is

Empty or its "0". If it is so, i have to ask input from the user and it should

be inbetween 1 to 100. i wrote code like this

DO until 1<AA<100
  if cells(9,3).value="" or cells(9,3).value="o" then 
     AA=InputBox("cells(9,3).value", "Enter only values  as  Numeric  ", "give the value Here")
  else
      AA= cells(9,3).value
  end if
loop

but its not doing after do until it skipping all the steps. please help.

Upvotes: 2

Views: 661

Answers (1)

KodornaRocks
KodornaRocks

Reputation: 405

The problem with the code is that everytime the condition (1 < AA < 100) will return true:

The code interpretation will be like this

C1 = (1 < AA)
C2 = C1 < 100

So, if:
AA > 1 Then C1 = 1 (True as VBA don't use strong types operators)
And if:
AA < 1 Then C1 = 0 (False as VBA don't use strong types operators)
Annnnd if:
AA is text, then C1 = 1 (Every non numeric character is bigger than an integer for VBA)
Then:
C1 = 1 or 0 or 1

The second condition will be:
C2 = C1 < 100
then 
if C1 = 0 or if C1 = 1 both are less than 100, it causes that C2 will aways be true.

All you need to do is to use three conditions, like:

Do Until IsNumeric(AA) And 1 < AA And AA < 100

Upvotes: 5

Related Questions