Visual Basic Validation

bt504

New Member
Joined
Nov 10, 2005
Messages
1
I'm struggling to create a validation check on visual basic. I am currently using an input form on VB which copies the information entered on the form onto a cell on a sheet on Excel.

I have 2 areas where a number is typed, in both are textboxes. I need to have a validation check to check the range (0-50) and to check the value entered is a number and is an integer.

I cannot use the validation checks used on cells in Excel as I am using an input form on VB.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try this:

Code:
Private Sub TextBox1_Change()
    Dim Char As String
    Char = UCase(Right(TextBox1.Text, 1))
    If Char Like "#" And Val(TextBox1.Text) <= 50 Then
        Exit Sub
    Else
        Beep
        MsgBox "Please enter an integer in the range 0 to 50"
        On Error Resume Next
        TextBox1.Text = Left(TextBox1.Text, Len(TextBox1.Text) - 1)
        TextBox1.SelStart = Len(TextBox1.Text)
    End If
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,586
Messages
5,838,230
Members
430,535
Latest member
krazykram

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
Top