数组中的通配符search

我有一个数组,其中包含一堆用于表示该字段正在等待PO的常用术语。 然后我有一个循环,通过大量的数据列删除任何行中单元格中的值不匹配数组中的任何项的行后退。 要做到这一点,我使用一个函数(我在网上find)来testing数组是否存在。

到目前为止,只要单元格中的值与数组中的值完全匹配,那么效果就非常好。 出错的地方是,如果一个单元格在一个常用术语(即“TBC-稍后”或者甚至仅仅是“TBC”而不是“TBC”)上包含轻微的变化,

我需要一种方法来获取单元格中的值,并对数组中的值进行通配符search。 我不会粘贴所有的代码(现在是一个中间开发的混乱),但是如果我们能够在下面得到这个工作,我可以应用它。

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 

这个函数现在返回“No!…”,因为“tbc xyz”不在数组中,但是如果有意义的话,我希望它返回“Yes!…”作为“tbc *”。

任何帮助感激地赞赏。

 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语句是相反的,对不起。 让我用一个案例和冗余匹配程序来进行弥补,并通过一个testing来显示它的重要性。

 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 

这段代码似乎是做你所需要的:

 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