UserForm Validation

switters_aka

Board Regular
Joined
Oct 26, 2010
Messages
118
I'm having this annoying problem and would appreciate some help if anyone has the time. I am running validation code on a userform to check if an entry is valid. I am calling the sub from a beforeupdate event on the textbox. The check works perfectly other than the .set focus which is not setting the focus on the offending textbox. The solution must be obvious but not to a newbie like me.

Code:
        If IsNumeric(UserForm2.eupTB5.Value) Then
        Exit Sub
        Else
        MsgBox "This is not a valid number"
        With UserForm2.eupTB5
        .Value = ""
        .SetFocus
        End With
        End If
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If your using the BeforeUpdate sub, try this...


Code:
If IsNumeric(UserForm2.eupTB5.Value) Then
    Exit Sub
Else
    MsgBox "This is not a valid number"
    UserForm2.eupTB5.Value = ""
    [COLOR="Red"]Cancel = True[/COLOR]
End If
 
Upvote 0
Why is the code in a separate sub?

If it was in the BeforeUpdate event then you could use the Cancel parameter.
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

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