2015-05-11 90 views
1

我有一個數組,包含一堆用於表示該字段正在等待PO的常用術語。然後我有一個循環,通過大量數據列向後走,刪除單元格中的值與數組中任何項不匹配的任何行。要做到這一點,我使用一個函數(我在網上找到)來測試數組中是否存在該值。數組中的通配符搜索

到目前爲止,只要單元格中的值與數組中的值完全匹配,就好了。出錯的地方是如果一個單元格在一個常用術語中含有輕微的變化(即,「TBC-稍後將會」,或者甚至僅僅是「TBC」而不是「TBC」),我就需要一種方法來獲取單元格中的值並對數組中的值執行通配符搜索。我不會粘貼我所有的代碼(現在這是一箇中間開發的混亂),但如果我們可以在下面得到這個工作,我可以應用它。

Sub TestFilterArray() 
MyArray = Array("tbc", "awaiting po", "po to follow") 
If IsInArray("tbc xyz", MyArray) = False Then 
    MsgBox "No! Item is not in the array" 
Else 
    MsgBox "Yes! Item is in the array" 
End If 
End Sub 

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

這個當前返回「不!......」爲「TBC某某」不是數組中存在,但我想它返回「是!...」爲「TBC *」在那裏,如果這就說得通了。

感謝任何幫助。

+0

你要使用部分匹配什麼規則?例如,如果'stringToBeFound'只是'po',應該返回'True'還是'False'?如果要返回「真」,那麼我們考慮「MyArray」中的多詞短語中的哪些詞,以及我們忽略了哪些詞? –

+0

謝謝你跳進來幫忙@RonRosenfeld非常好的一點!理想情況下,stringToBeFound中的整個字符串應該在兩端用通配符進行計算。所以「等待po-xyz」將返回true,但「等待xyz po」將返回false。 –

+0

請參閱我的答案中的代碼 –

回答

1
Function IsInArray2(StringToBeFound As String, MyArray As Variant) As Boolean 
IsInArray2 = False 
For i = LBound(MyArray) To UBound(MyArray) 
    If "*" & MyArray(i) & "*" Like StringToBeFound Then IsInArray2 = True 'will match MyArray to any substring of StringToBeFound 
Next 
End Function 

考慮到運行時的考慮變得

Function IsInArray2(StringToBeFound As String, MyArray As Variant) As Boolean 
IsInArray2 = False 
For i = LBound(MyArray) To UBound(MyArray) 
    If "*" & MyArray(i) & "*" Like StringToBeFound Then 
     IsInArray2 = True 'will match MyArray to any substring of StringToBeFound 
     Exit Function 
    End If 
Next 
End Function 

謝謝你的言論。回顧一下,是的Like聲明反過來,對不起。讓我用一個案例和冗餘匹配器來彌補它,並通過一個測試來展示它的重要性。

Function IsInArray2(stringToBeFound As String, MyArray As Variant) As Boolean 
IsInArray2 = False 
For i = LBound(MyArray) To UBound(MyArray) 
    If LCase(stringToBeFound) Like LCase("*" & Replace(MyArray(i), " ", "*") & "*") Then 
     IsInArray2 = True 'will match MyArray to any substring of StringToBeFound 
     Exit Function 
    End If 
Next 
End Function 

Sub TestFilterArray() 
MyArray = Array("coca cola gmbh", "awaiting po", "po to follow") 
If IsInArray2("Coca Cola Deutschland GmbH", MyArray) = False Then 
    MsgBox "No! Item is not in the array" 
Else 
    MsgBox "Yes! Item is in the array" 
End If 
End Sub 
+0

感謝!這看起來很棒,我可以看到它應該如何工作,但由於某種原因,它仍然返回「不!」。在if語句中逐句通過Like操作符不會返回true,因此跳過Then到Next ...任何想法? –

+0

剝離到它的基本級別,在即時窗口中的以下內容返回「否」 - 如果「* tbc *」像「tbc」然後MsgBox「是」否則MsgBox「否」 –

2

此代碼似乎做你需要什麼:

Option Explicit 

Sub TestFilterArray() 
Dim MyArray As Variant 
MyArray = Array("tbc", "awaiting po", "po to follow") 
If arrMatch("tbc xyz", MyArray) = False Then 
    MsgBox "No! Item is not in the array" 
Else 
    MsgBox "Yes! Item is in the array" 
End If 
End Sub 

    Function arrMatch(stringToSearch As String, Arr As Variant) As Boolean 
    Dim sS As String, sF As String 
    Dim I As Long 

sS = " " & Trim(stringToSearch) & " " 
For I = LBound(Arr) To UBound(Arr) 
    sF = "*" & Trim(Arr(I)) & "*" 
    If sS Like sF Then 
     arrMatch = True 
     Exit Function 
    End If 
Next I 

arrMatch = False 
End Function 
+1

感謝@ronrosenfeld與給定參數。但是,當我更改'如果arrMatch(「tbc xyz」''如果arrMatch(「tbc-xyz」'我回到不匹配... –

+0

從您的規範中我不清楚你想要也可以匹配MyArray中的字符串,但它很容易改變 –

+0

@HarleyB查看edit'd版本是否適合您 –