Turning NUMLOCK on at the end of a macro run

前端 未结 4 1886
暖寄归人
暖寄归人 2021-01-06 07:01

What code does: I have a code that moves the mouse around the screen, takes printscreens and pastes it to excel.

Problem: For some

4条回答
  •  太阳男子
    2021-01-06 07:53

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

    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))...

    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 "Capslock" instead of Numlock if you need it so as the case may be.

    Thanks. Sashi Elit :)

提交回复
热议问题