2017-03-29 2 views
0

J'ai trouvé cette fonction pour identifier si une chaîne est dans un tableau donné, mais il ne semble pas être capable de gérer les caractères génériques (ou au moins pas comme je le fais il).Vérifier si la chaîne est dans le tableau ou ne pas inclure les caractères génériques avec Excel VBA

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) 
End Function 

Mon utilisation de la fonction:

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 
+1

Cela semble être ce que vous cherchez: http://stackoverflow.com/questions/30175061/wildcard-search-in-array – SLWS

+0

Oui et non. Certaines de mes chaînes ne sont pas des jokers tandis que d'autres le sont. celui auquel vous êtes lié ne semble pas le distinguer. Cela étant dit, je suppose que le moyen le plus simple serait d'utiliser deux tableaux avec et sans caractères génériques –

Répondre

1

Ou quelque chose comme:

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 
0

Merci SLWS. J'ai modifié le code que vous avez référencé un peu afin de répondre à mes besoins.
Cela fonctionne pour moi:

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