简体   繁体   English

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

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

I am new to VBA...looking for code that will only allow me to enter a value in a column if the value in one or more of the three cells immediately to the left "contains" the word "Other". 我是VBA的新手,正在寻找仅允许我在紧邻左侧的三个单元格中的一个或多个单元格中的值“包含”单词“其他”的情况下在列中输入值的代码。 I've successfully written the code so that if the value in one or more of the cells is "Other" I am restricted from entering a value, but have not been successful in using ISERROR and FIND so that the code looks for text that includes "other". 我已经成功编写了代码,因此,如果一个或多个单元格中的值是“其他”,则我不能输入值,但是使用ISERROR和FIND未能成功,因此代码查找包含以下内容的文本“其他”。 Here is what I have right now... 这就是我现在所拥有的...

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

Any suggestions would be most appreciated! 任何建议将不胜感激!

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

You can use COUNTIF with a wildcard to look for at least once cell containing other , ie: 您可以将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.

相关问题 当单元格中的值超过另一个时,如何在excel vba中显示消息框? - How to Display a message box in excel vba when value in a cell exceeds another? 如果单元格的值超过另一个单元格的值,则显示消息框弹出窗口的VBA代码 - VBA code to show Message Box popup if the value of a cell exceeds the value of another 如果特定单元格包含特定文本,则VBA代码可重新复制多个单元格 - VBA code to recopy multiple cells if a specific cell contains a specific text Vba宏如果单元格包含值,则在其他单元格上输入特定文本 - Vba macro if cell contains value, then input specific text on other cells 当下拉框更改值时,使用VBA代码从单元格复制特定值 - Using VBA code to Copy Specific value from a cell when the dropdown box changes that value 如果相应的单元格值等于条件,则显示单元格值的消息框 - Display message box of cell value if a corresponding cell value is equal to criteria Excel VBA选择一个单元格区域,直到一个单元格包含特定文本 - Excel VBA Select a Range of Cells Until a Cell Contains Specific Text VBA-根据单元格是否包含特定文本隐藏行 - VBA- Hide rows based on whether cell contains specific text 使用VBA在Excel中的文本框中显示单元格内容 - Display cell content in a text box in excel using VBA VBA将单元格值复制到另一个单元格文本字符串中 - VBA to copy cell value into another cell text string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM