Reputation: 1
I am trying to validate a TextBox in a Userform. The user can only enter in the format of xx.x.
For example 34.5 is acceptable.
If the user enters 3.4 a MsgBox is shown and the user is asked to enter again.
My problem is the MsgBox is showing when the user enters the correct format (for example 45.6).
I am using the BeforeUpdate function for the TextBox.
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
my_format = Format("00.0")
If Not my_format Then
MsgBox " xx.x is the required format.", vbCritical
Cancel = True
End If
End Sub
I think the problem is on the line: If Not my_format Then ....
I am trying to say:
If TextBox.Value is not equal to my_format show a msg.
Upvotes: 0
Views: 314
Reputation: 42256
Please, use this updated code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim my_format As String: my_format = "##.#"
If Not TextBox2.Value Like my_format Then
MsgBox " xx.x is the required format.", vbCritical
Cancel = True
End If
End Sub
You need to check the Text Box value against the format. The format itself does not have too much meaning in the necessary circumstances...
Upvotes: 1