Search code examples
excelvbaeventsonclickcell

action when a cell is clicked


H! I am new to VBA. This maybe a too simple of a question, but I am struggling to use VBA: when a cell(1,1) is clicked as it has 1, the msgbox would show up saying "hi"

Sub test()
  'click action 'when cell(1,1) is clicked and if cells(1,1) is 1, msgbox saying "hi" will show up, if not nothing If Cells(1, 1) = 1 Then
  MsgBox "hi"
  Else 
End If
End Sub

Solution

  • This code belongs in the worksheet module.

    Right click on the sheet tab and select "View Code"

    Paste this code there:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Address = "$A$1" Then
            If Target = 1 Then
                MsgBox "hi"
            End If
        End If
    End Sub