Macro turning off Number Lock

lemeister

New Member
Joined
Nov 20, 2003
Messages
48
I have run the following piece of code numerous times a day for the past few years without any problems (it basically uses the send keys to run an excel add in to extract data from an AS400 database).
However, we recently upgraded to excel 2007, and now each time I run the code, it turns off the number lock. I have no idea why it is doing this. Could it be the sequence of sendkey commands or something else?? I'm very confused and it's getting a little frustrating!
Code:
Private Sub CommandButton1_Click()
With Range("TimeStamp")
    .Select
    .FormulaR1C1 = "=now()"
    .Copy
    .PasteSpecial xlPasteValues
End With

Range("User").Value = Environ("Username")

Range("DataStart:$CC$20000").ClearContents
Range("DataStart").Select

SendKeys "%{x}"
SendKeys "+{r}"
SendKeys "+{n}"
SendKeys "{ENTER}"
SendKeys "EQUATORRE-PROD.QBE.EO"
SendKeys "{ENTER}"
SendKeys Range("Queryname").Value
SendKeys "{ENTER}"
SendKeys "{ENTER}"
SendKeys "{ENTER}"
End Sub
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I use Sendkeys quite a lot but am not familiar with your code like
SendKeys "%{x}". What does this do ? It may be the cause of the problem.

Curly brackets normally mean that a special key is required like your lower code .. SendKeys "{ENTER}"
 
Upvote 0
I use Sendkeys quite a lot but am not familiar with your code like
SendKeys "%{x}". What does this do ? SendKeys "{ENTER}"

The SendKeys "%{x}" is a keyboard shortcut for Alt x - it's basiscally a shortcut to the the Add-Ins tab in Excel 2007.
 
Upvote 0
There used to be a bug with multiple sendkeys statements in a row (see this MSKB article) - perhaps it has reappeared.
 
Upvote 0
Code:
Private Declare Sub keybd_event Lib "user32" ( _
ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Private Const VK_NUMLOCK = &H90
Private Const KEYEVENTF_KEYUP = &H2
Declare Function GetKeyState Lib "user32.dll" ( _
ByVal nVirtKey As Long) As Integer

Sub test()
 'NUM_Off
 NUM_On
End Sub

Sub NUM_TOGGLE()
  'Toggle NUM-Lock key state
  keybd_event VK_NUMLOCK, 1, 0, 0
  keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
End Sub

Sub NUM_On()  'Turn NUM-Lock on
  If Not (GetKeyState(vbKeyNumlock) = 1) Then
    keybd_event VK_NUMLOCK, 1, 0, 0
    keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  End If
End Sub

Sub NUM_Off() 'Turn NUM-Lock off
  If (GetKeyState(vbKeyNumlock) = 1) Then
    keybd_event VK_NUMLOCK, 1, 0, 0
    keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
  End If
End Sub
 
Upvote 0
Thanks folks, that has shed a little light on the issue.

That code works perfect (although I cannot confess to understanding most it!)
 
Upvote 0
Hey There!!!

First of all, Copy and paste the following code in your Excel Sheet’s Module (Ex:-Module-1)...

Code:
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
           Private Const kCapital = 20
           Private Const kNumlock = 144
 
           Public Function CapsLock() As Boolean
           CapsLock = KeyState(kCapital)
           End Function
 
           Public Function NumLock() As Boolean
           NumLock = KeyState(kNumlock)
           End Function
 
           Private Function KeyState(lKey As Long) As Boolean
           KeyState = CBool(GetKeyState(lKey))
           End Function
Then, Copy and Paste the following in your Sheet's Code (Ex:- Sheet1 (Code)...

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          Range("XFD1").FormulaR1C1 = "=NumLock()"
          If Range("XFD1").Value = "FALSE" Then
          SendKeys "{NUMLOCK}"
          Else
          End If
          End Sub
Now Chill!!! For Each SelectionChange you make, Excel Refreshes itself and It makes sure that Numlock is On Always.
Replace
Code:
Capslock
instead of
Code:
Numlock
if you need it so as the case may be.


Thanks. Elit :)
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top