ABOUTYOU
如果您想知道是否在数组中找到该字符串,请尝试以下函数:Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)End Function正如肖恩·柴郡指出的那样,这必须是一维数组。例:Sub Test() Dim arr As Variant arr = Split("abc,def,ghi,jkl", ",") Debug.Print IsInArray("ghi", arr)End Sub(以下代码根据HansUp的评论进行了更新)如果要在数组中匹配元素的索引,请尝试以下操作:Function IsInArray(stringToBeFound As String, arr As Variant) As Long Dim i As Long ' default return value if value not found in array IsInArray = -1 For i = LBound(arr) To UBound(arr) If StrComp(stringToBeFound, arr(i), vbTextCompare) = 0 Then IsInArray = i Exit For End If Next iEnd Function这也假设一维数组。请记住,LBound和UBound从零开始,因此索引为2表示第三个元素,而不是第二个。例:Sub Test() Dim arr As Variant arr = Split("abc,def,ghi,jkl", ",") Debug.Print (IsInArray("ghi", arr) > -1)End Sub如果您有特定的示例,请使用它来更新您的问题,否则示例代码可能不适用于您的情况。