[英]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.