excelvbanum-lock

Basic Excel VBA turns number lock off, I've no idea why


This is the code that I use regularly fired from a Button (Form Control) that is stored in PERSONAL.XLSB and therefore available universally on this PC.

Sub Today()

    Dim Today As Date
    
    Today = Date
    
    ActiveCell.Value = Today
    
    Application.SendKeys "{TAB}"
    
End Sub

Immediately after using that button the number lock is turned off.


Solution

  • It's a known issue. You could try using shell's SendKeys instead:

    Sub Today()
    
        Dim Today As Date
        
        Today = Date
        
        ActiveCell.Value = Today
    
        Dim WshShell As Object            
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.SendKeys "{TAB}", True
        
    End Sub
    

    However, as FunThomas points out in comments - there are better ways of manipulating the 'cursor'.