VBA根据单元格值弹出MsgBox

VBA MsgBox pop-up according to cell value

我正在尝试将电子表格设置为 return 一个消息框,每当输入一个单元格值时,我有下面的代码,它只适用于 1 个单元格。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range
    Set A = Range("D5")
    If Intersect(Target, A) Is Nothing Then Exit Sub
    If Target.Value = "Yes" Then
        MsgBox "Message"
    End If
End Sub

只要满足这 3 个中的任何一个,我就需要弹出一个 MsgBoxD5="Yes"B5="No"B13="Submit form",但是我做不到在给定时间弹出超过 1 个单元格,或者写入一个模块或 3 个单独的模块。每个单元格的每个框都应该 return 不同的消息,即 D5="Yes" MsgBox "Please fill out..." 或 B5="No" MsgBox "Submit Form"

只需按顺序将所有测试包含在事件中。

此代码还将同时处理多个单元格更改(即 Target 包含多个单元格),而您的原始代码不会

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range

    Set A = Me.Range("D5")
    If Not Intersect(Target, A) Is Nothing Then
        If A = "Yes" Then
            MsgBox "Message for D5"
        End If
    End If

    Set A = Me.Range("B5")
    If Not Intersect(Target, A) Is Nothing Then
        If A = "No" Then
            MsgBox "Message for B5"
        End If
    End If

    Set A = Me.Range("B13")
    If Not Intersect(Target, A) Is Nothing Then
        If A = "Submit form" Then
            MsgBox "Message for B13"
        End If
    End If
End Sub