Defining input box data type?

B

Big Chris

Hi,

I'm running the macro below and everything seemed OK.....I enter th
wrong numberic code I get a "Number is incorrect" message and it stops
I enter the right numeric code and it and goes on and runs anothe
macro.
Problem is, if I the user enters an incorrect alph code (e.g. ABCD) i
bombs out and shows the error as.... "Run-Time error 13 - Typ
Mismatch".
Could anyone please tell me how I can avoid this from happening, an
instead get it to produce the same "Number is incorrect" message?

When the macro interupts it highlights the line
'If CLng(myNum) = ActiveCell.Value Then'.

Many thanks!!

Chris

---------------------------------------------------------------------------

Sub CommandButton1_Click()

myNum = InputBox("Please enter your 8 digit validation code. :", "Ente
Code")

If myNum = "" Then
Exit Sub
End If

Sheets("Sheet1").Select
Range("a3").Select

If CLng(myNum) = ActiveCell.Value Then
Application.OnTime Now, "RunAnotherMacro"

Else
MsgBox "Number is incorrect"

End If

Sheets("Sheet1").Select
Range("A1").Value = myNum

End Su
 
T

Tom Ogilvy

Sub CommandButton1_Click()

myNum = InputBox("Please enter your 8 digit validation code. :", "Enter
Code")

If myNum = "" Then
Exit Sub
End If

Sheets("Sheet1").Select
Range("a3").Select

if Isnumeric(myNum) then
If CLng(myNum) = ActiveCell.Value Then
Application.OnTime Now, "RunAnotherMacro"

Else
MsgBox "Number is incorrect"
exit Sub
End If
Else
msgBox "Number is alphanumeric"
Exit Sub
End If

Sheets("Sheet1").Select
Range("A1").Value = myNum

End Sub
 
H

Hank Scorpio

Hi,

I'm running the macro below and everything seemed OK.....I enter the
wrong numberic code I get a "Number is incorrect" message and it stops.
I enter the right numeric code and it and goes on and runs another
macro.
Problem is, if I the user enters an incorrect alph code (e.g. ABCD) it
bombs out and shows the error as.... "Run-Time error 13 - Type
Mismatch".
Could anyone please tell me how I can avoid this from happening, and
instead get it to produce the same "Number is incorrect" message?

There are two types of InputBox. One is the InputBox function that
you're using. The other is the InputBox method of the Excel
application object. The latter type lets you specify the type of
input. If the user puts the wrong type in, it generates an error. To
see it in action, try this:

Sub ApplicationIPBDemo()

Dim vnt As Variant
Dim l As Long

'The 1 argument tells the box that you
'want a numeric value.
vnt = Application.InputBox _
("Please enter a number.", , , , , , , 1)

If vnt = False Then Exit Sub

l = CLng(vnt)

End Sub
 
J

John Wilson

Big Chris,

A lot of ways (and variations) to do this.

Replace the section of code:

If myNum = "" Then
Exit Sub
End If

with this:

If IsNumeric(mynum) Then
If Len(mynum) < 8 Then
MsgBox "Not enough Numbers"
Exit Sub
End If
If Len(mynum) > 8 Then
MsgBox "Tooo!!! many Numbers"
Exit Sub
End If
Else
MsgBox "Doh!!! Don't use letters"
Exit Sub
End If

John
 
B

Big Chris

Thanks guys! You've all been very helpful and I now have a nice choice
to make and can maybe add one or two features.

Your time and knowledge is much appreciated!

Regards,
 
Top