VBA Not Equal Statement

hayden

Board Regular
Joined
Sep 23, 2005
Messages
188
Hello,

I'm trying to write a vba statement to check that the user has entered an integer in my userform text box between 3 and 6. This is what I have so far:

Code:
If TestNumBox.value <> 3 Or TestNumBox <> 4 Or TestNumBox <> 5 Or TestNumBox <> 6 Then
'TestNumBox is not an integer between 3 and 6, need a message alerting user.
MsgBox "The number of steps in the test must be an integer between 3 and 6.", , "Please check the number of steps in test."
TestNumBox.SetFocus
Exit Sub
Else 'Everything is filled in correctly. Exit if statment and unload the BasicData form.
End If
[\code]

As far as I know my if statment says if the the testnumbox (i.e. the textbox value is not equal to 3, or not equal to 4 or not equal to 5 or not equal to 6 then do the message box otherwise continue. 

But it always goes into the message box routine and never exits the if.  

Any ideas?

Thanks Hayden
 
{snip}
tusharm - thanks for your explanation. I would assume that I could also use code similar to this to vailidate a date or a time if required? I would guess that IsNumeric could also be isDate?

Guessing is sometimes required but only if looking up help for 'isdate' doesn't work. In this case, you would not have to guess. {grin}
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This is likely a precision problem or a data type problem. Use the Round(num,#decimals) function if the data types are the same. Otherwise, try to convert and compare similar data types. I use cInt() or cDbl() or similar funtion.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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