我是VBA的新手...寻找只允许我在列中输入值的代码,如果左边三个单元格中的一个或多个单元格中的值“包含”单词“Other”。我已成功编写代码,以便如果一个或多个单元格中的值为“其他”,则限制输入值,但未成功使用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
任何建议都会非常感激!
答案 0 :(得分:1)
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
答案 1 :(得分:0)
您可以使用带有通配符的COUNTIF
来查找至少一个包含其他的单元格,即:
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