SerenityNetworks
Board Regular
- Joined
- Aug 13, 2009
- Messages
- 131
- Office Version
- 365
- Platform
- Windows
I'm using the following code to capture key press events when the specified keys on the 10-key pad are pressed. It works perfectly, with one exception. When I'm in column 3 I do not want the key presses to do anything. I want normal key and cell behavior. Here is what is happening...
I have struggled all day trying to figure out what's happening.
Any help will be greatly appreciated.
- I enter a 1 (from the 10-key pad) in cell A2
- The OnKey event picks up the key I pressed
- It enters a 1 in cell A2
- It skips to cell B2 and enters a date/time value
- It skips to cell C2
- I type a 2 (from the 10-key pad) in cell C2
- The value 2 enters just fine
- I type an "a"
- The 2 is replaced with the "a". Ugh! I want the cell to now have "2a", not just "a".
- However, if I first enter an "a" then I can enter a 2 and the cell will show "2a" just as intended.
- If I type "222222" (in column 3), it looks like it is entering fine, but as soon as I type an "a" (or any non-trigger character) the 2s go away and are replaced by the "a".
I have struggled all day trying to figure out what's happening.
Any help will be greatly appreciated.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Sheets("Hidden").Range("C2").Value = "Running" Then
If ActiveCell.Column = 3 Then
'normal behavior
Else
Application.OnKey "{ENTER}", "MyEnterEvent" ' Enter key on 10-key pad
Application.OnKey "{96}", "MyNumberEvent0" ' 0 key
Application.OnKey "{97}", "MyNumberEvent1" ' 1 key
Application.OnKey "{98}", "MyNumberEvent2" ' 2 key
Application.OnKey "{99}", "MyNumberEvent3" ' 3 key
Application.OnKey "{100}", "MyNumberEvent4" ' 4 key
Application.OnKey "{101}", "MyNumberEvent5" ' 5 key
Application.OnKey "{102}", "MyNumberEvent6" ' 6 key
Application.OnKey "{103}", "MyNumberEvent7" ' 7 key
Application.OnKey "{104}", "MyNumberEvent8" ' 8 key
Application.OnKey "{105}", "MyNumberEvent9" ' 9 key
Application.OnKey "{107}", "MyNumberEventPlus" ' + key
End If
Else
Application.OnKey "{107}", "MyNumberEventPlus" ' + key
End If
End Sub