Private Sub Workbook_Open()
Dim WBname As String
WBname = ThisWorkbook.name
If Not InStr(WBname, "test") > 0 Then
MsgBox ("NotOK")
End If
End Sub
编辑:更多说明。
我现在测试是否"测试"在工作簿名称中。
但是我想测试更多的单词而不仅仅是"测试"在工作簿名称中没有复制粘贴代码一千次。
答案 0 :(得分:5)
如果WBname
必须包含所有字词
Dim WBname As String
WBname = ThisWorkbook.Name
Dim arrWords As Variant, aWord As Variant
arrWords = Array("aa", "bb", "cc") 'input your words list here
For Each aWord In arrWords
If Not InStr(WBname, aWord) > 0 Then
MsgBox ("NotOK")
Exit For
End If
Next
如果WBname
必须包含至少一个字
Dim WBname As String
WBname = ThisWorkbook.Name
Dim arrWords As Variant, aWord As Variant
arrWords = Array("aa", "bb", "cc") 'input your words list here
Dim wordFound As Boolean
wordFound = False
For Each aWord In arrWords
If InStr(WBname, aWord) > 0 Then
wordFound = True
Exit For
End If
Next
If Not wordFound Then
MsgBox ("NotOK")
End If
答案 1 :(得分:0)
您可以在IF中使用AND:
If Not InStr(WBname, "test") > 0 and not InStr(WBname, "other word") ...
或者您可以使用OR,具体取决于您的测试。
答案 2 :(得分:0)
使用Excel公式的方法更短,速度更慢:
containsAny = Evaluate("count(search({""ab"",""cd""},""bcde""))>0") ' True
或者您的样本:
Private Sub Workbook_Open()
If Evaluate("count(search({""test"",""Book1""},""" & Me.Name & """))=0") Then
MsgBox "NotOK"
End If
End Sub