隐藏在Excel VBA中弹出的其他消息框

时间:2016-08-09 20:42:52

标签: excel vba excel-vba

以下代码查找列中的某些文本字符串,并在某些内容匹配时给出一个msg框。代码查找多个文本字符串,所以如果我有" X"和" y"在一列中,代码查找两个文本字符串,然后会出现两个msg框。我只希望第一个msg框显示并隐藏其余部分。有没有办法做到这一点?

换句话说,代码查找多个文本字符串,如果文本字符串匹配则弹出msg框。不止一个文本字符串肯定会匹配,但我只想显示第一个框,并隐藏其余部分。

由于

   Private Sub Worksheet_Change(ByVal Target As Range)

Dim icounter As Long
Dim icounter1 As Long
Dim lastrow As Long

Dim MSG As String, ans As Variant
For icounter = 2 To 31

    If Cells(icounter, 2) = "Job Code Name" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

ElseIf Cells(icounter, 2) = "Personnel Area" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

ElseIf Cells(icounter, 2) = "Line of Sight" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

ElseIf Cells(icounter, 2) = "Title of Position" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

ElseIf Cells(icounter, 2) = "Company Code Name" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

ElseIf Cells(icounter, 2) = "Function" Then
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")

Else

End If

Next icounter
End Sub

3 个答案:

答案 0 :(得分:3)

如果我理解你的问题,你可以使用Select Case而不是所有If ... ElseIf的东西。只是阅读评论。显然你也想退出For循环,所以添加Exit For

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icounter As Long
    Dim icounter1 As Long
    Dim lastrow As Long

    Dim MSG As String, ans As Variant
    For icounter = 2 To 31
        Select Case Cells(icounter, 2)
            Case "Job Code Name"
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
            Case "Personnel Area"
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
            Case "Line of Sight"
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
            Case "Title of Position" 
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
            Case "Company Code Name" 
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
            Case "Function" 
                MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
                Exit For
        End Select
    Next icounter
End Sub

答案 1 :(得分:0)

我建议您在代码中进行另一次重构。由于您使用的是Worksheet事件,因此每次更改单元格内容时都会触发该事件,因此我相信您在For循环之前添加Application.EnableEvents = False,然后在它之后添加Application.EnableEvents = True。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icounter As Long
    Dim icounter1 As Long
    Dim lastrow As Long
Dim MSG As String, ans As Variant

Application.EnableEvents = False
For icounter = 2 To 31
    Select Case Cells(icounter, 2)
        Case "Job Code Name"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
        Case "Personnel Area"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
        Case "Line of Sight"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
        Case "Title of Position"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
        Case "Company Code Name"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
        Case "Function"
            MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed")
            Exit For
    End Select
Next icounter

Application.EnableEvents = False
End Sub

答案 2 :(得分:0)

我使用了蒂姆的答案,但只要我对表格中的任何单元格进行编辑,就会触发它。为避免这种情况,我在Select Case Cells(icounter, 2)之前添加了这行代码:

If Not Intersect(Target, Cells(icounter, 2)) Is Nothing Then

此编辑仅在单元格更改为我输入的字符串文本时弹出msg框,即公司代码名称