VBA - if cell contains a word, then messagebox just one single time

Viewed 644

My idea was to get an alert every time I digit the word "high" in a cell of column A (also if the word is contained in a longer string). This alert should pop up just if i edit a cell and my text contains "high" and I confirm (the alert shows when I press "enter" on the cell to confirm or just leave the edited cell). So I made this code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsError(Application.Match("*high*", Range("A:A"), 0)) Then
       MsgBox ("Please check 2020 assessment")
End If
End Sub

The code seemed working fine. I digit "high" in a cell of column A and get the alert when I confirm- leave the cell. The problem is that when i have a single "high" cell, the alert continues to pop up at every modification I do, in every cell. So is impossible to work on the sheet. I need a code to make sure that, after digiting "high", i get the alert just one time, and then I do not get others when editing any cell, unless i digit "high" in another cell, or i go and modify a cell that already contains "high" and I confirm it again. What could I do? Thanx!!

3 Answers

This will set a target (monitored range) and check if the first cell changed contains the word

Be aware that if you wan't to check every cell changed when you modify a range (for example when you copy and paste multiple cells), you'r have to use a loop

Private Sub Worksheet_Change(ByVal Target As Range)
    
    ' Set the range that will be monitored when changed
    Dim targetRange As Range
    Set targetRange = Me.Range("A:A")
    
    ' If cell changed it's not in the monitored range then exit sub
    If Intersect(Target, targetRange) Is Nothing Then Exit Sub
    
    ' Check is cell contains text
    If Not IsError(Application.Match("*high*", targetRange, 0)) Then
        ' Alert
        MsgBox ("Please check 2020 assessment")
    End If
    
End Sub

Let me know if it works

I tried your code; now, if column "A" has a cell "high", the alert correctly pop up and if then I edit cells in a column other than column "A", I don't get alert, so this is the good news!

The bad news is that if I have one single "high" in column A, when I edit any other cell in column "A" itself, I still get the alert everytime.

A Worksheet Change: Target Contains String

  • The message box will show only once whether you enter one ore multiple criteria values.

The Code

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const srcCol As String = "A"
    Const Criteria As String = "*high*"
    
    Dim rng As Range: Set rng = Intersect(Columns(srcCol), Target)
    If rng Is Nothing Then
        Exit Sub
    End If
    
    Application.EnableEvents = False
    
    Dim aRng As Range
    Dim cel As Range
    Dim foundCriteria As Boolean
    For Each aRng In rng.Areas
        For Each cel In aRng.Cells
            If LCase(cel.Value) Like LCase(Criteria) Then
                MsgBox ("Please check 2020 assessment")
                foundCriteria = True
                Exit For
            End If
        Next cel
        If foundCriteria Then
            Exit For
        End If
    Next aRng
    
    Application.EnableEvents = True
    
End Sub

Sub testNonContiguous()
    Range("A2,A12").Value = "high"
End Sub
Related