string="abc"arr=Array("cyb","dbv","ero","eu","fxf","gbb","jyn","udn","uup","fxa","fxb","fxc","fxe","fxy")如何判断string字符串是否等于arr其中某字符串?...
string = "abc"
arr = Array("cyb", "dbv", "ero", "eu", "fxf", "gbb", "jyn", "udn", "uup", "fxa", "fxb", "fxc", "fxe", "fxy")
如何判断string字符串是否等于arr其中某字符串?
以下是可以直接运行的代码,请试用。
需要注意的是,Array函数给arr数组赋值后,arr数组的下界是0,而不是1,可以用Lbound(arr)检测。如果在For...Next循环中从1开始循环,当所要查找的字符正好是arr(0)时,则会漏掉。相应的,如果发现有字符匹配,则数组中应该是第i+1个元素与目标字符串匹配。
不知以上的说明是否清楚,希望对你有所帮助。
Sub stringMatch()
Dim arr As Variant
Dim i As Integer, s As String
Dim matchLabel As Boolean
matchLabel = False
s = "abc"
arr = Array("cyb", "dbv", "ero", "eu", "fxf", "gbb", "jyn", "udn", "uup", "fxa", "fxb", "fxc", "fxe", "fxy")
For i = LBound(arr) To UBound(arr)
If arr(i) = "abc" Then
MsgBox "数组arr中第" & i+1 & "个元素等于" & s
matchLabel = True
End If
Next i
If matchLabel = False Then
MsgBox "数组arr中没有元素等于" & s
End If
End Sub