简体   繁体   中英

VBA pop up message if cell contains text

I have an problem.

I have created a sheet, where data for different ranges is pulled from another sheets using many different formulas.

Now, in one of cell range (D11:G15), the formula gives names of approvers, yet, in the original document, their names always have prefixes sometimes numbers, sometimes numbers and letters ie.:WL11-Adam Smith, 12-Adam Smith etc. Now, we have been introduced with a new process that requires additional actions if one of approver is a particular person:

thus I need a pop out message box with instructions that appears if the string in above mentioned range contains names of those approvers , let's say Adam Smith and Diana Rose, but as I mentioned the names in range appear through formula and they contain not only name itself but also a series of other characters. Additionally, this macro should be triggered automatically if a person appears in given range.

在此处输入图像描述

This is my sheet. So in general, our agents, input just country and commodity code, all the rest is pulled from other documents. As you can see the range with approvers are in column D to G but it can be various rows from 1 to 4,6 rows.

Updated code:

Private Sub Worksheet_Change(ByVal Target As Range)   
Dim people, c As Variant people = Array("Adam Smith", "Diana Rose") 
For Each c In Worksheets("Sheet1").Range("D11:G15") 
For Each person In people If c.Value2 Like "*" & person & "*" Then 
MsgBox ("Your instructions") 
End If 
Next person 
Next c  
End Sub

I get error Type mismatch (Error 13) on line: If c.Value2 Like "*" & person & "*" Then

If anyone could help with this I would be extremely grateful.

PS. I tried doing it with data validation but unfortunately it doesn't work, I also tried doing it with if function that appears in an empty cell, but the instructions are simply too long and could not finish it that way either.

There's not a lot wrong with the code you've posted, other than a couple of missing line breaks and a declare.

However, if there is a cell within the tested range that contains an error, it will throw an error 13. You can trap this with the IsError function like so:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim people As Variant, person As Variant, c As Variant
    people = Array("Adam Smith", "Diana Rose")
    For Each c In Worksheets("Sheet1").Range("D11:G15")
        For Each person In people
            If IsError(c) Then
                ' do nothing or advise there is a cell error
            Else
                If c.Value2 Like "*" & person & "*" Then
                    MsgBox ("Your instructions")
                End If
            End If
        Next person
    Next c
End Sub

If you only want the code to execute when a change has been made within the test range and only report once per change - then you could use this:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim people As Variant, person As Variant, c As Variant, testrange As Range
    Set testrange = Intersect(Worksheets("Sheet1").Range("D11:G15"), Target)
    If Not (testrange Is Nothing) Then
        people = Array("Adam Smith", "Diana Rose")
        For Each c In testrange
            For Each person In people
                If IsError(c) Then
                    ' do nothing or advise there is a cell error
                Else
                    If c.Value2 Like "*" & person & "*" Then
                        MsgBox ("Your instructions")
                    End If
                End If
            Next person
        Next c
    End If
End Sub 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM