【问题标题】:VBA MsgBox pop-up according to cell value根据单元格值弹出VBA MsgBox
【发布时间】:2018-08-08 03:10:43
【问题描述】:

我正在尝试设置一个电子表格,以便在输入单元格值时返回一个消息框,下面的代码仅适用于 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

只要满足以下三个条件中的任何一个,我都需要弹出一个MsgBoxD5="Yes"B5="No"B13="Submit form",但是我不能让它在给定的一个单元格中弹出超过 1 个时间要么写在一个模块中,要么写在 3 个单独的模块中。每个单元格的每个框都应返回不同的消息,即 D5="Yes" MsgBox "请填写..." 或 B5="No" MsgBox "Submit Form"

【问题讨论】:

    标签: vba msgbox


    【解决方案1】:

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

    此代码还将同时处理多个单元格更改(即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
    

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多