我想要最快捷的方法(在Excel中的VBA)来确定是否有一个string发生在另一个地方 – 一个'包括'function也许?

我想要最快捷的方法(在Excel中的VBA)来确定是否有一个string发生在另一个地方 – 一个'包括'function也许?

我相信INSTR()是函数; 如果返回零以外的任何string,则会find该string。

exists = InStr("avbprogram", "vb") <> 0 

Carl是正确的,但你也应该知道InStr的默认比较选项是区分大小写的。 如果你想做大小写不敏感的检查,你应该把你的参数包装在LCase / UCase中,或者使用InStr函数的扩展forms,如下所示:

 exists = InStr(1, "avbprogram", "vb", vbTextCompare) 

第一个参数是开始比较的第一个字符的索引,最后一个参数表示不区分大小写的比较。 卡尔展示的短手实际上相当于下面显示的内容:

 exists = InStr(1, "avbprogram", "vb", vbBinaryCompare) 

另一种select是使用Like运算符。

以下MSDN使用指南很有帮助:

 Dim testCheck As Boolean ' The following statement returns True (does "F" satisfy "F"?) testCheck = "F" Like "F" ' The following statement returns False for Option Compare Binary ' and True for Option Compare Text (does "F" satisfy "f"?) testCheck = "F" Like "f" ' The following statement returns False (does "F" satisfy "FFF"?) testCheck = "F" Like "FFF" ' The following statement returns True (does "aBBBa" have an "a" at the ' beginning, an "a" at the end, and any number of characters in ' between?) testCheck = "aBBBa" Like "a*a" ' The following statement returns True (does "F" occur in the set of ' characters from "A" through "Z"?) testCheck = "F" Like "[AZ]" ' The following statement returns False (does "F" NOT occur in the ' set of characters from "A" through "Z"?) testCheck = "F" Like "[!AZ]" ' The following statement returns True (does "a2a" begin and end with ' an "a" and have any single-digit number in between?) testCheck = "a2a" Like "a#a" ' The following statement returns True (does "aM5b" begin with an "a", ' followed by any character from the set "L" through "P", followed ' by any single-digit number, and end with any character NOT in ' the character set "c" through "e"?) testCheck = "aM5b" Like "a[LP]#[!ce]" ' The following statement returns True (does "BAT123khg" begin with a ' "B", followed by any single character, followed by a "T", and end ' with zero or more characters of any type?) testCheck = "BAT123khg" Like "B?T*" ' The following statement returns False (does "CAT123khg"?) begin with ' a "B", followed by any single character, followed by a "T", and ' end with zero or more characters of any type?) testCheck = "CAT123khg" Like "B?T*"