Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You might try
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If Not (KeyAscii = Asc("0") And KeyAscii _ <= Asc("9") Or KeyAscii = Asc(".")) Then KeyAscii = 0 End If End Sub -- Regards, Tom Ogilvy " wrote: 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 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Tom,
Cheerssssss.... That's work... thanks ... But I have to modify to reject "." if already there ... I use "." for indicating value after decimal And not permit the user press "." if already pressed after decimal And also I want : If the first left Textbox.value is "." so textbox1.value= "0." + blablabla is numeric But I already finish it... Thanks for your great and simple advise .... Best Regards, Halim Tom Ogilvy menuliskan: You might try Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If Not (KeyAscii = Asc("0") And KeyAscii _ <= Asc("9") Or KeyAscii = Asc(".")) Then KeyAscii = 0 End If End Sub -- Regards, Tom Ogilvy " wrote: 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 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Tom,
I'm now using these following code for my textbox: In my past post, I want to enabled CTRL+S in my TextBox1 I'm no longer using API calls to detect CTRL keydown, I use: Private Sub TextBox1_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger, ByVal Shift As Integer) If Shift = 2 And KeyCode = 83 Then KeyCode = 0 MsgBox "YOU PRESSED: CTRL + S" End If End Sub For disabling unless 0-9 char and enabling only "." char I use and directly format TEXTBOX1.value to decimal and after decimal I use: Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If InStr(1, TextBox1.Value, ".") < 0 Then If Not (KeyAscii = Asc("0") And KeyAscii _ <= Asc("9") And Not KeyAscii = Asc(".")) Then KeyAscii = 0 If Left(TextBox1.Value, 1) = "." Then _ TextBox1.Value = Application.Substitute(TextBox1.Value, ".", "0.", 1) Else If Not (KeyAscii = Asc("0") And KeyAscii _ <= Asc("9") Or KeyAscii = Asc(".")) Then KeyAscii = 0 End If End Sub Thanks a lot for your suggestions. Regards, Halim menuliskan: Hi Tom, Cheerssssss.... That's work... thanks ... But I have to modify to reject "." if already there ... I use "." for indicating value after decimal And not permit the user press "." if already pressed after decimal And also I want : If the first left Textbox.value is "." so textbox1.value= "0." + blablabla is numeric But I already finish it... Thanks for your great and simple advise .... Best Regards, Halim Tom Ogilvy menuliskan: You might try Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If Not (KeyAscii = Asc("0") And KeyAscii _ <= Asc("9") Or KeyAscii = Asc(".")) Then KeyAscii = 0 End If End Sub -- Regards, Tom Ogilvy " wrote: 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 |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
keydown event | Excel Programming | |||
keydown event | Excel Programming | |||
user form-on open event? keydown event? | Excel Programming | |||
KeyDown event in sheet | Excel Programming | |||
keydown event | Excel Programming |