LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 113
Default Ogilvy or someone else please advise me!!!, KeyDown event

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

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
keydown event Peter T Excel Programming 0 April 18th 05 02:03 PM
keydown event K Dales[_2_] Excel Programming 0 April 18th 05 12:46 PM
user form-on open event? keydown event? FSt1[_3_] Excel Programming 2 August 5th 04 02:26 PM
KeyDown event in sheet Peter Wallin Excel Programming 1 February 27th 04 10:25 AM
keydown event jim c. Excel Programming 1 October 10th 03 03:16 AM


All times are GMT +1. The time now is 05:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"