QFarley
QFarley

Reputation: 91

Trigger VBA script when any 1 cell selected containing a defined string

The code below worked fine so long as the "MainSubroutine" only needed to be triggered when cell G14 was clicked on, but now I need to widen the trigger event to whenever any target cell on the sheet is clicked on and it has the specific string, "Click to Learn More".

So how do I write that? I tried altering line 2 to "If Selection.Count = 1 AND selection.value = "Click to Learn More" Then..." but obviously that didn't work.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("G14")) Is Nothing Then
            Call MainSubroutine
        End If
    End If
End Sub

Upvotes: 1

Views: 507

Answers (1)

SJR
SJR

Reputation: 23081

This should do it? If it can contain the string use Instr or similar.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If target.Count = 1 Then
        If Target.value="Click to learn more" Then
            Call MainSubroutine
        End If
    End If
End Sub

Upvotes: 1

Related Questions