gpt4 book ai didi

excel - InStr 值数组(可能?)

转载 作者:行者123 更新时间:2023-12-04 21:15:29 40 4
gpt4 key购买 nike

Private Sub Workbook_Open()

Dim WBname As String
WBname = ThisWorkbook.name

If Not InStr(WBname, "test") > 0 Then
MsgBox ("NotOK")
End If

End Sub

编辑:更多说明。

我现在测试工作簿名称中是否包含“Test”。

但我想测试工作簿名称中是否有比“测试”更多的单词,而无需复制粘贴代码一千次。

最佳答案

如果 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

关于excel - InStr 值数组(可能?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43784022/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com