Input Box Validation

backfromthebush

New Member
Joined
Jul 2, 2010
Messages
37
Hi all,

I have a form which asks the user for a contract number via an input box.

The format of this number should be xxx-xxxxxxx-xxx.

Is there a code I can add that will validate the number so that it is entered in the above format e.g. 123-1234567-123.

If the contract number is correct it will skip to the next part of the code. If it is incorrect then it will bring up an error message giving the user the option to override the error, or return to the contract number input box.

Any suggestions? :biggrin:
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Something like this perhaps
Code:
Sub test()
        Dim uiString As String
        uiString = "123-1234567-123"
NumberEntry:
        uiString = Application.InputBox("Enter", Default:=uiString, Type:=2)
        If uiString = "False" Then Exit Sub: Rem cancel
        
        If Not (uiString Like "[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]") Then
            Select Case MsgBox("entry does not match the pattern", vbAbortRetryIgnore)
                Case vbAbort
                    Exit Sub
                Case vbRetry
                    GoTo NumberEntry
                Case vbIgnore
                    Rem 'next part of the code'
            End Select
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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