2015-05-11 48 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版本是否适合您 –