PDA

View Full Version : inputbox variable validation



y2mash
09-13-2007, 01:04 PM
Hi need some help on this issue!

If i have a variable as variant, which is determined by the result of an input box entry, what vba code do i need to determine whether the variable is text or not! for example

Answer1 = inputbox("please answer the first question")
if not istext(Answer1) Then
MsgBox "please enter text only"
Exit Sub
End If

I need something like this..but this does not seem to be working for me!

Many thanks!

Amash

mikerickson
09-13-2007, 01:32 PM
If you use Application.Input box rather than InputBox,

Answer1 = Application.InputBox("Please answer the first question", type:=2)

Excel will perform the validations by itself.

Or are you counting the string "2" as "not text"? If so, are "5a" or "x3" text?

y2mash
09-13-2007, 03:40 PM
If you use Application.Input box rather than InputBox,

Answer1 = Application.InputBox("Please answer the first question", type:=2)

Excel will perform the validations by itself.

Or are you counting the string "2" as "not text"? If so, are "5a" or "x3" text?

Thanks for your help, but im having difficulty with that code. When i declare my variable (Answer1) as a string or even a variant and run the code and set is as type:2, it allows me to enter numbers into the inputbox, although when i set the type to type:=1 then it only allows numbers to be entered.

Thanks.

Amash.

mikerickson
09-13-2007, 04:00 PM
With type:=2, it will not let you enter the number 2, although it will let you enter the numeral (string) "2".

One validation method to not allow numerals to be entered might be



Dim Answer1 As String
Do
Answer1 = Application.InputBox("Please answer the first question.", Type:=2)
If "False" = Answer1 Then MsgBox "Cancel Pressed": Exit Sub
If 0 < Val(Answer1) Then MsgBox "No numerals allowed"
Loop Until 0 = Val(Answer1)This has some quirks (it will reject an entry of "12xyz"). Whether those are a problem depends on what your requirements are. And they can be ajusted to meet your needs.