Quote Originally Posted by Johnyc View Post
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:
  1. Private Sub TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  2.     If Val(TextBox.Text) < 0.03 Then
  3.         MsgBox "cannot less than 0.03!", vbCritical
  4.         '~~> reset the invalid number
  5.         TextBox.Text = ""
  6.     End If
  7. End Sub
  8.  
  9. Private Sub TextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  10.     Dim pos As Long
  11.    
  12.     '~~> Check if the text already has a decimal
  13.     pos = InStr(1, TextBox, ".")
  14.    
  15.     If pos > 0 And KeyAscii = 46 Then
  16.         KeyAscii = 0
  17.     ElseIf Not Chr(KeyAscii) Like _
  18.     "[0-9,.]" And KeyAscii <> 8 Then
  19.         KeyAscii = 0
  20.     End If
  21. End Sub