Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 452
Yong Avatar asked Dec 07 '25 01:12

Yong


1 Answers

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
like image 142
Davesexcel Avatar answered Dec 09 '25 09:12

Davesexcel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!