How to apply conditions on Input box?

A

aiyer

Hi all!

An input boxprompts the user to specify a numeric value for a certain
variable "mynum", as follows. The user CANNOT type in
gibberish/characters. It has to be only numbers -ve.0. + ve numbers
only are acceptable.

==============================================
mynum = InputBox("Enter the area multiplication factor")

If mynum = " " Then
Exit Sub
End If
==============================================

But the above logic accepts characer inputs/gibberish as well.
Look forward to your helps guys.

Thanks alot,

Arun...
Vtec. Corp.
 
N

Norman Jones

Hi Aiyer,

Try:

mynum = Application.InputBox _
("Enter the area multiplication factor", Type:=1)
 
O

Otto Moehrbach

Aiyer
The easiest way I have found is to simply check the response for the
condition I want. Something like:
Dim MyNum as Variant
TryAgain:
MyNum = InputBox("Enter the area multiplication factor")
If Not IsNumeric(MyNum) Then
MsgBox "The multiplication factor entered is not numeric. Please
enter it again.",,"Invalid Entry"
GoTo TryAgain
End If

The above will repeat the query until the user enters a numeric value. You
can add other checks as well like, for instance, the value has to be between
this and that. HTH Otto
 
G

Gord Dibben

Try

If Mynum = "" Or Not IsNumeric(Mynum) Then

take action here

Gord Dibben Excel MVP
 
D

Dave Peterson

You can make the user enter a number using application.inputbox with type:=1.

And you can check for a "cancel" this way:

Option Explicit
Sub testme01()

Dim myNum As Variant
myNum = Application.InputBox _
("Enter the area multiplication factor", Type:=1)

If myNum = False Then
'user cancelled
Else
MsgBox myNum
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top