DRJ
MrExcel MVP
- Joined
- Feb 17, 2002
- Messages
- 3,853
Hi
I want to restric a textbox to only numbers. I use
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
on the keypress event
now the number will be formatted at "00.00" So I want to automatically put a decimal in so I use
Private Sub TextBox2_AfterUpdate()
Select Case Len(TextBox2.Value)
Case 3
TextBox2.Value = TextBox2.Value / (10)
Case 4
TextBox2.Value = TextBox2.Value / (100)
Case Else
End Select
TextBox2.Value = Format(TextBox2.Value, "00.00")
End Sub
This works fine. But what if the user puts a decimal in. I can allow kaypress(46) to allow the decimal, but then the user could put 2 decimals in by mistake like 2..50
So what I would really like is if there is no decimal put one in. If there is one leave it there. If there are more than one delete all but the first.
Kind of like the way the windows calculator works. You can put a decimal in whenever you want, but after you do you cant put in another.
Thanks
Jacob
I want to restric a textbox to only numbers. I use
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
on the keypress event
now the number will be formatted at "00.00" So I want to automatically put a decimal in so I use
Private Sub TextBox2_AfterUpdate()
Select Case Len(TextBox2.Value)
Case 3
TextBox2.Value = TextBox2.Value / (10)
Case 4
TextBox2.Value = TextBox2.Value / (100)
Case Else
End Select
TextBox2.Value = Format(TextBox2.Value, "00.00")
End Sub
This works fine. But what if the user puts a decimal in. I can allow kaypress(46) to allow the decimal, but then the user could put 2 decimals in by mistake like 2..50
So what I would really like is if there is no decimal put one in. If there is one leave it there. If there are more than one delete all but the first.
Kind of like the way the windows calculator works. You can put a decimal in whenever you want, but after you do you cant put in another.
Thanks
Jacob