Data Validation in Userform, Checking String is Numeric and Seven Digits Long

vba_monkey

Board Regular
Joined
Dec 18, 2013
Messages
112
Hello,

I am trying to validate entries into a userform field to check that it only contains numbers and is 7 digits long. All i need it to do is highlight the field in red if the entry is not correct but not if the field is exited without entering anything. This is what I have so far:

Code:
Private Sub SJNKE_Policy_1_TB_Exit(ByVal Cancel As MSForms.ReturnBoolean)
     If Not IsNumeric(SJNKE_Policy_1_TB.Text) And Len(SJNKE_Policy_1_TB.Text) <> 7 Or SJNKE_Policy_1_TB.Text <> "" Then
        SJNKE_Policy_1_TB.BackColor = &HFF&
     Else
        SJNKE_Policy_1_TB.BackColor = &H80000005
     End If
End Sub

The results from this are:

If non numeric characters are included the field turns red, which is correct.
If a number less than or more than 7 digits is entered the field stays white, which is wrong.
If the field is activated but then exited without entering anything the field turns red, which is wrong.

Any ideas please?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Slightly different to requirements , but try this:-
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  If -CLng(Chr(KeyAscii) Like "[0-9]") = 0 Then
    MsgBox "Please Enter Numbers Only"
    KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
  ElseIf Len(TextBox1.Text) = 7 Then
    KeyAscii = 0
  End If
End Sub
 
Upvote 0
Hello,

I am trying to validate entries into a userform field to check that it only contains numbers and is 7 digits long. All i need it to do is highlight the field in red if the entry is not correct but not if the field is exited without entering anything. This is what I have so far:

Code:
Private Sub SJNKE_Policy_1_TB_Exit(ByVal Cancel As MSForms.ReturnBoolean)
     If Not IsNumeric(SJNKE_Policy_1_TB.Text) And Len(SJNKE_Policy_1_TB.Text) <> 7 Or SJNKE_Policy_1_TB.Text <> "" Then
        SJNKE_Policy_1_TB.BackColor = &HFF&
     Else
        SJNKE_Policy_1_TB.BackColor = &H80000005
     End If
End Sub

The results from this are:

If non numeric characters are included the field turns red, which is correct.
If a number less than or more than 7 digits is entered the field stays white, which is wrong.
If the field is activated but then exited without entering anything the field turns red, which is wrong.
See if this replacement for what you posted does what you want...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub SJNKE_Policy_1_TB_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If Len(SJNKE_Policy_1_TB.Text) = 0 Or SJNKE_Policy_1_TB.Text Like "#######" Then
    SJNKE_Policy_1_TB.BackColor = &H80000005
  Else
    SJNKE_Policy_1_TB.BackColor = &HFF&
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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