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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,684
Messages
5,637,790
Members
416,982
Latest member
lisam77

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