Home » excel » Trigger event on TAB button click in Excel with VBA

Trigger event on TAB button click in Excel with VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to set up a verification in an excel cell , so that nobody can leave it empty by clicking a tab button.

So as soon as a user clicks the tab button with the empty cell , it should display a message.

Do You have any clue for that?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If iKeyCode = vbKeyTab Then    
    If CancSelEvnt = False Then    
        MsgBox (1)        
    Else 
        If IsEmpty(Cells(Target.Row, 7).Value) = True Then   
            MsgBox ("This cell should not be kept empty.")    
        End If       
    End If    
End If

End Sub

Just want any error message as soon as user moves from one cell to another cell in same row (using tab key)

How to&Answers:

Run this command:

Sub EnableTab()
    Application.OnKey "{TAB}", "TabPressed"
End Sub

And whenever Tabis pressed, the TabPressed Sub would be executed:

Sub TabPressed()
    Debug.Print "MsgBox!"
End Sub

To disable it run this:

Sub DisableTab()
    Application.OnKey "{TAB}", ""
End Sub

If it should be in a workbook, then consider writing Application.OnKey in the events of the workbook: