Search code examples
excelvba

Everytime I run a VBA Macro, it disables my NUM LOCK


At first, I thought it was an issue with Windows 10, however, after trying several solutions I gave up until I coincidentally realized that running a VBA macro was disabling and enabling my NUM-KEY.

Sub NewLineInventory()

Dim ws As Worksheet
Dim isProtected As Boolean

Set ws = ThisWorkbook.Sheets("Inventory")

' Checks if the worksheet is protected
isProtected = ws.ProtectContents

    ' Use error handling to ensure the worksheet is protected again
    On Error Resume Next

    ' Code to make edits in the worksheet
    Range("A4").EntireRow.Insert

    Range("A5:W5").Copy
    Range("A4").PasteSpecial
    Range("E4").ClearContents
    Range("R4").ClearContents
    Range("T4").ClearContents
    Range("A4") = "No"

    SendKeys ("{ESC}")

    ' Reset error handling
    On Error GoTo 0

End Sub

I tried removing any code line related to protection and it the issue disappears. Does anyone have any idea why?

FYI. The protection code lines are added to allow VBA to make edits to the worksheet without requiring a password.

Thanks in advance.


Solution

  • It's a known bug.

    Sendkeys "{ESC}" actually hits ESCAPE and NUMLOCK, it would seem.

    You can put Sendkeys "{NUMLOCK}" on the next line after the Sendkeys "{ESC}" to revert it.

    However, if you're just using ESCAPE to clear the after-paste visual, you don't need the Sendkeys at all, you can just use:

    Application.CutCopyMode = False