在特定单词输入单元格时,在Excel中弹出消息

时间:2016-11-08 15:35:20

标签: excel vba excel-vba

我正在尝试创建一个弹出消息,只有当某个单词出现在电子表格的一系列单元格中时才会出现。目前,我写的宏会在输入任何内容时显示弹出消息。这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("G10:G40")) Is Nothing Then
        MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication.  This can affect both pipe costs and leadtime."
    End If
End Sub

同样,我只想在DURA-CORE II出现在单元格范围内时出现弹出窗口。我现在承认我对VBA几乎一无所知,所以我确信这个修复很简单。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

这样的东西?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("G10:G40")) Is Nothing Then
        If Target = "DURA-CORE II" Then
            MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication.  This can affect both pipe costs and leadtime."
        End If
    End If
End Sub

答案 1 :(得分:1)

你可以使用

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Range("G10:G40").Find(what:="DURA-CORE II", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) Is Nothing Then
        MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication.  This can affect both pipe costs and leadtime."
    End If
End Sub
只要Range("G10:G40")中的任何单元格都有内容" DURA-CORE II"

,就会以这种方式通知用户。