Problems do while with multiple criteria

bbaixinho

New Member
Joined
Mar 20, 2016
Messages
2
Hi everyone,

I am trying to use a Do while with multiple conditions for an inputbox but it doesn't work.

My code:
'---------------------
dim x as variant
dim y as variant
Do While x - y <= 0 Or x-y > 30 or isnumeric(x)=false
x = InputBox("Invalid value. Please insert")
If StrPtr(numberclaimyear) = 0 Then 'when the user choose the option cancel in inputbox
Exit Sub
End If
Loop


'--------
this code is not working when I insert a letter. I don't understand why because if I use only the condition "do while isnumeric(x)=false" and I insert 1 letter it works..


Thanks in advance!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi bb, welcome to the forum

So, trying to break this down. Using a variant in this case may be the issue. If you enter a letter then this statement will fail: x-y. Because x is no longer a number when you enter a letter into the input box. You may want to test for "isNumeric" first.

I actually like to store the Inputbox into a string, test to see if it's a number, convert it to a value and store it into a number variable.

Try this
Code:
  Dim v As Variant  Dim X As Single
  Dim Y As Single
  
  Do
    v = InputBox("Invalid value. Please insert")
    If Len(v) = 0 Then Exit Do
    If IsNumeric(v) = True Then
      X = Val(A)
      If X - Y > 0 Or X - Y <= 30 Then Exit Do
    End If
  Loop
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top