
Originally Posted by
Johnyc
thanks Koolsid,
It is such a great code, it works fine in the VB program. but when i try to implement in the Visual Basic Excel, it seems like the LostFocus event cannot take effect, so do i need to change any command on that if i want to implement this code in VBA excel?
thanks in advance again!
For VBA use this
vb Code:
Private Sub TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Val(TextBox.Text) < 0.03 Then
MsgBox "cannot less than 0.03!", vbCritical
'~~> reset the invalid number
TextBox.Text = ""
End If
End Sub
Private Sub TextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim pos As Long
'~~> Check if the text already has a decimal
pos = InStr(1, TextBox, ".")
If pos > 0 And KeyAscii = 46 Then
KeyAscii = 0
ElseIf Not Chr(KeyAscii) Like _
"[0-9,.]" And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub