检查字符串是否在数组中或不包括 Excel VBA 的通配符
Check if string is in array or not including wildcards with Excel VBA
我发现这个函数可以识别一个字符串是否在给定的数组中,但是它似乎不能处理通配符(或者至少不能像我那样处理)。
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
我使用的功能:
Dim BannedWildcards() As Variant
BannedWildcards = Array("", "-", "?", 0, "na", "n/a", _
"*account*", "*hse*", "*defined*", "*applicable*", "*operation*", "*action*", "*manager*")
Select Case True
Case IsInArray(LCase(Sht_Tracker.Cells(RowCounter_CRCT, 17)), BannedWildcards) = True
Arr_Dashboard_Current(10, ArrC_Dashboard_Current) = "#N/A"
Case Else: Arr_Dashboard_Current(10, ArrC_Dashboard_Current) = Sht_Tracker.Cells(RowCounter_CRCT, 17)
End Select
或类似的东西:
Function IsInArray2(StringToBeFound As String, MyArray As Variant) As Boolean
IsInArray2 = False
wildCard = "*"
If InStr(StringToBeFound, wildCard) > 0 Then
For i = LBound(MyArray) To UBound(MyArray)
If "*" & MyArray(i) & "*" Like StringToBeFound Then IsInArray2 = True 'will match MyArray to any substring of StringToBeFound
Next
Else
For i = LBound(MyArray) To UBound(MyArray)
If MyArray(i) == StringToBeFound Then IsInArray2 = True 'will exactly match MyArray to StringToBeFound
Next
End If
End Function
谢谢SLWS。为了满足我的需要,我修改了你引用的代码。
这对我有用:
Function IsInArray(stringToBeFound As String, MyArray As Variant) As Boolean
Dim i As Long
Dim WildCard As String
WildCard = "*"
IsInArray = False
For i = LBound(MyArray) To UBound(MyArray)
If InStr(MyArray(i), WildCard) > 0 Then
If LCase(stringToBeFound) Like LCase("*" & Replace(MyArray(i), " ", "*") & "*") Then
IsInArray = True
Exit Function
End If
Else
If LCase(stringToBeFound) = LCase(MyArray(i)) Then
IsInArray = True
Exit Function
End If
End If
Next
End Function
我发现这个函数可以识别一个字符串是否在给定的数组中,但是它似乎不能处理通配符(或者至少不能像我那样处理)。
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
我使用的功能:
Dim BannedWildcards() As Variant
BannedWildcards = Array("", "-", "?", 0, "na", "n/a", _
"*account*", "*hse*", "*defined*", "*applicable*", "*operation*", "*action*", "*manager*")
Select Case True
Case IsInArray(LCase(Sht_Tracker.Cells(RowCounter_CRCT, 17)), BannedWildcards) = True
Arr_Dashboard_Current(10, ArrC_Dashboard_Current) = "#N/A"
Case Else: Arr_Dashboard_Current(10, ArrC_Dashboard_Current) = Sht_Tracker.Cells(RowCounter_CRCT, 17)
End Select
或类似的东西:
Function IsInArray2(StringToBeFound As String, MyArray As Variant) As Boolean
IsInArray2 = False
wildCard = "*"
If InStr(StringToBeFound, wildCard) > 0 Then
For i = LBound(MyArray) To UBound(MyArray)
If "*" & MyArray(i) & "*" Like StringToBeFound Then IsInArray2 = True 'will match MyArray to any substring of StringToBeFound
Next
Else
For i = LBound(MyArray) To UBound(MyArray)
If MyArray(i) == StringToBeFound Then IsInArray2 = True 'will exactly match MyArray to StringToBeFound
Next
End If
End Function
谢谢SLWS。为了满足我的需要,我修改了你引用的代码。
这对我有用:
Function IsInArray(stringToBeFound As String, MyArray As Variant) As Boolean
Dim i As Long
Dim WildCard As String
WildCard = "*"
IsInArray = False
For i = LBound(MyArray) To UBound(MyArray)
If InStr(MyArray(i), WildCard) > 0 Then
If LCase(stringToBeFound) Like LCase("*" & Replace(MyArray(i), " ", "*") & "*") Then
IsInArray = True
Exit Function
End If
Else
If LCase(stringToBeFound) = LCase(MyArray(i)) Then
IsInArray = True
Exit Function
End If
End If
Next
End Function