一个专用子控件下的两个对话框功能

时间:2018-08-30 14:20:20

标签: excel-vba dialog

我试图让两个对话框在Private Sub Worksheet_Change(ByVal Target as Range)下工作。只要有人在范围内输入某个值,它们就会弹出。第一个工作正常,但第二个根本不显示。下面是我正在使用的代码。

    Private Sub Worksheet_Change(ByVal Target As Range)
  If Intersect(Target, Range("J2:J54")) Is Nothing Then Exit Sub
  If Target.Value <> "Transportation" Then Exit Sub
     MsgBox "TRANSPORTATION: Remember tolls."
  If Target.Value <> "Guiding" Then Exit Sub
     MsgBox "GUIDING: Remember to add 10%."
End Sub

有人可以帮我看看我要去哪里了吗

2 个答案:

答案 0 :(得分:1)

您将在每个点退出潜艇。尝试扭转逻辑,即:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("J2:J54")) Is Nothing Then Exit Sub
    If Target.Value = "Transportation" Then
        MsgBox "TRANSPORTATION: Remember tolls."
    ElseIf Target.Value = "Guiding" Then
        MsgBox "GUIDING: Remember to add 10%."
    End If
End Sub

答案 1 :(得分:1)

另一种可能性是使用Select Case,先检查If Not Intersect... Is Nothing,而不是如果If Intersect...Is Nothing退出子。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("J2:J54")) Is Nothing Then
        Select Case Target.Value
            Case "Transportation"
                MsgBox "TRANSPORTATION: Remember tolls."
            Case "Guiding"
                MsgBox "GUIDING: Remember to add 10%."
        End Select
    End If
End Sub