Home » excel » excel vba – Macro to detect clicking on a cell in a specific row

excel vba – Macro to detect clicking on a cell in a specific row

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to figure out how to get a macro to run if I click on any cell in a specific row in Excel.

Let’s say I want to show a message “hello world” if I click on any cell in row 4. I tried looking around, but only got as far as the follows:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("4")) Then
            MsgBox "Hello World"
        End If
    End If
End Sub

Obviously this is wrong, but any help in the right direction would be appreciated. Thanks.

How to&Answers:

This should work:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Row = 4 Then
            MsgBox "Hello World"
    End If
End Sub