H
halimnurikhwan
Hi Ogilvy and else,
I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0
'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)
'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub
If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub
'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub
by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...
Is my code efficient enough ?, or do you can make it more simple ?!
Regards,
Halim
I've made this :
Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer
Private Const VK_SHIFT = &H10
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
'if Shift key was pressed
If GetKeyState(VK_SHIFT) < 0 Then KeyCode = 0
'if there's a decimal or dot on the first then fill 0 before it
If Left(TextBox1.Value, 1) = "." Then _
TextBox1.Value = Application.Substitute(TextBox1.Value, ".",
"0.", 1)
'to pass if ESC, Enter, Delete, Arrow Down or up, "." (dot) was
pressed
If KeyCode = 13 Or KeyCode = 27 Or KeyCode = 40 Or KeyCode = 38 Or
_
KeyCode = 46 Or KeyCode = 8 Or Chr(KeyCode) Like "[0-9]" Or _
(KeyCode >= 96 And KeyCode <= 105) Or _
(KeyCode = 190 Or KeyCode = 110) And _
InStr(1, TextBox1.Value, ".") = 0 Then Exit Sub
If Chr(KeyCode) Like "[A-Z]" Or _
KeyCode = 32 Or (KeyCode >= 106 And KeyCode <= 221) _
Or KeyCode = 190 Then KeyCode = 0
End Sub
'then :
'change format of Textbox value after we fill it by (0 to 9)
Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Value, ".") > 0 Then
TextBox1.Value = FormatNumber(TextBox1.Value,
Len(TextBox1.Value) - InStr(1, TextBox1.Value, "."))
Else
TextBox1.Value = FormatNumber(TextBox1.Value, 2)
End If
End Sub
by that code all I want to achieve is disabled keyboard press unless
the keyboard pressed was
0 to 9 ...
Is my code efficient enough ?, or do you can make it more simple ?!
Regards,
Halim