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

以下代码查找某列中的某些文本string,并在匹配时提供一个msg框。 该代码查找多个文本string,所以如果我在一列中有“X”和“y”,并且代码查找两个文本string,则会出现两个msg框。 我只想要第一个味精盒来显示和隐藏其余的。 有没有办法做到这一点?

换句话说,代码查找多个文本string,如果文本string匹配,则popupmsg框。 不止一个文本string肯定会匹配,但我只想要第一个框出现,并隐藏其余的。

谢谢

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 

如果我理解你的问题,你可以使用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 

我会推荐你​​在你的代码重构。 由于您正在使用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 

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

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

只有当单元格变为我input的string文本,即公司代码名称时,此编辑才会popupmsg框