Check on the input in an inputbox

T

Tempy

Good day,
I am requesting the user to enter a ten digit number into the input box
and would like to test this and if not ten digits, then they must
re-enter.
Could somebody please help me with this, im sure simple code.

thanks in advance

Tempy

*** Sent via Developersdex http://www.developersdex.com ***
 
N

Neil

Put your number in to string variable eg A$

Use the following code to test the length of the string

If Len(A$) <>10 Then
MsgBox ("Incorrect number")
'Do something else to make your user re-enter the number
End If

Once added to your code the user will only be able to proceed when the
variable is ten characters long. Note - this will only check for string
length not validity, so letters and numbers can both be used, you will need
to add seperate validation for those.

HTH

Neil
www.nwarwick.co.uk
 
T

Tom Ogilvy

Sub ab()
Dim bContinuetoLoop As Boolean
Dim res As String, sStr As String
Dim msg As String
msg = "enter 10 digit number"
Do
bContinuetoLoop = False
res = Application.InputBox(msg, Default:=sStr)
If Len(res) <> 10 Then
bContinuetoLoop = True
msg = "Entry is not 10 digits" & _
vbNewLine & vbNewLine & _
"Enter a 10 digit number:"
ElseIf Not IsNumeric(res) Then
bContinuetoLoop = True
msg = "Entry is not a Number" & _
vbNewLine & vbNewLine & _
"Enter a 10 digit number:"
End If
If res = "" Then Exit Sub
sStr = res
Loop Until Not bContinuetoLoop
MsgBox res
End Sub
 
D

David

Hi,
Try this, hope it works for your purposes
Sub Macro1()
Dim Message, Title, MyValue
Message = "Please enter the 10 digit number"
Title = "10 digit number"
MyValue = InputBox(Message, Title)
If MyValue Like "##########" Then
'Do nothing
Else
Stop
Dim Msg1
Msg1 = "Please reenter a 10 digit number"
Response = MsgBox(Msg1)
End
End If
End Sub

Thanks,
 
C

Charlie

If you do this often, as I do, you may like this "AllNumeric" function.
(There is an "IsNumeric" function, but it returns true for any numeric string
including minus signs and decimal points, etc.)


(main code...)

Dim MyInput As String
'
Do While Len(MyInput) <> 10 Or Not AllNumeric(MyInput)
MyInput = InputBox("Enter a 10-digit number")
Loop
'
MsgBox """" & MyInput & """"
'
(end main code...)

Private Function AllNumeric(txt As String) As Boolean
'
Dim Digit As String
Dim i As Long
'
For i = 1 To Len(txt)
Digit = Mid(txt, i, 1)
AllNumeric = Digit >= "0" And Digit <= "9"
If Not AllNumeric Then Exit Function
Next i
'
AllNumeric = True
'
End Function
 

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

Similar Threads


Top