繁体   English   中英

如果另一个单元格中的值包含特定文本,则显示消息框的VBA代码

[英]VBA code to display message box if value in another cell contains specific text

我是VBA的新手,正在寻找仅允许我在紧邻左侧的三个单元格中的一个或多个单元格中的值“包含”单词“其他”的情况下在列中输入值的代码。 我已经成功编写了代码,因此,如果一个或多个单元格中的值是“其他”,则我不能输入值,但是使用ISERROR和FIND未能成功,因此代码查找包含以下内容的文本“其他”。 这就是我现在所拥有的...

If Target.Column = 15 And Target <> "" Then
    If Cells(Target.Row, Target.Column - 1).Value <> "Other" _
        Or Cells(Target.Row, Target.Column - 2).Value <> "Other" _
        Or Cells(Target.Row, Target.Column - 3).Value <> "Other" _
        Then

        Target.Value = ""
        MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
        Exit Sub
   End If
End If
exitHandler:
  Application.EnableEvents = True

End Sub

任何建议将不胜感激!

If Target.Column = 15 And Target <> "" Then
    If InStr(1, Cells(Target.Row, Target.Column - 1).Value, "Other") = 0 _
        And InStr(1, Cells(Target.Row, Target.Column - 2).Value, "Other") = 0 _
        And InStr(1, Cells(Target.Row, Target.Column - 3).Value, "Other") = 0 _
        Then

        Target.Value = ""
        MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
        Exit Sub
   End If
End If
exitHandler:
  Application.EnableEvents = True
End Sub

您可以将COUNTIF与通配符一起使用,以查找至少一个包含other的单元格,即:

If target.Column = 15 And target.Value <> "" Then
 If Application.WorksheetFunction.CountIf(target.Offset(0, -3).Resize(1, 3), "*other*") = 0 Then
        target.Value = ""
        MsgBox "First Select 'Other' value in one or more of the 'Excluded Employee' Columns to the left"
        Exit Sub
 End If
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM