• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

If statement on userform textbox

Rodrigues

Member
Hi There
I'm sure this will be an easy one, but I'm scratching my head with it.
I'm using code attached the problem is that, pop the msg on every digit I type,
Need a code to only accept 6 digits anything out of lenght would like to stop user to proceed further until key in 6 digits
thanks R

Code:
Private Sub TextBox2_AfterUpdate()
If Len(TextBox2.Value) <> 6 Then
    MsgBox "Only allowed 6 digits"
Cancel = True
  End If
End Sub
 
Normally, I'd check not on the textbox itself but just before info is committed (i.e. when you click on submit button etc).

But if you want validation on the textbox itself, you'd do something like below.
Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Len(TextBox1.Value) <> 6 Or Not IsNumeric(TextBox1.Value) Then
        MsgBox "Only allowed 6 digits"
        Cancel = True
End If
End Sub
 
Back
Top