Vba Ubound(Filter())函数为部分字符串匹配返回true
下面的函数返回Vba Ubound(Filter())函数为部分字符串匹配返回true,vba,excel,filter,Vba,Excel,Filter,下面的函数返回true对于部分匹配,如何修改它以仅在整个字符串匹配时返回true 例如,如果阵列具有AAA,A,BB,B 如果我通过了AAA,那么A也会返回True Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr, stringToBeFound, , vbTextCompare)) > -1) End Function 是否
true
对于部分匹配,如何修改它以仅在整个字符串匹配时返回true
例如,如果阵列具有AAA
,A
,BB
,B
如果我通过了AAA
,那么A也会返回True
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound, , vbTextCompare)) > -1)
End Function
是否对此进行了简单的修改,或者我是否应该编写单独的代码来实现此功能?您可以使用标记来删除以下内容中的任何部分匹配项:
ary = Array("AAA", "A", "BB", "B")
让我们假设$字符没有出现在数组中的任何位置。
我们创建一个如下字符串:
st = "$" & Join(ary, "$") & "$"
也就是$AAA$A$BB$B$
如果我们想查看A是否在数组中,请检查InStr(st,$A$)>0
如果我们想查看AAA是否在数组中,请检查InStr(st,$AAA$)>0
如果$在数组中,请使用类似Chr(1)
的标记
编辑#1:
实施:
编辑#2:
用法:
因此,如果我们想查看字符串AA是否在数组中:
Sub MAIN()
ary = Array("AA", "BB", "CC", "", "DD")
MsgBox IsInArray("$AA$", ary)
End Sub
如果我们想查看是否有任何数组元素为空:
Sub MAIN()
ary = Array("AA", "BB", "CC", "", "DD")
MsgBox IsInArray("$$", ary)
End Sub
这个备选方案最接近原始问题“是否有简单的修改?”。它没有使用不同的方法,而是使用应用于相同(1dim)数组的相同过滤器函数:
语法过滤器(sourcearray,match[,include[,compare]]
其他功能
此外,它还允许在两者之间进行选择
- i) 整串搜索
- ii)部分匹配
测试代码并执行,而不会损失性能
代码
Public Function IsInArray(stringToBeFound As String, arr As Variant, Optional bFull = False) As Boolean
' Purpose: search strings in 1dim arrays using the Filter function
' i) whole strings (bFull = True) or
' ii) partial matches (bFull = False, default)
' Exclude empty strings "" (Note: if not wanted, comment the next line out!)
If stringToBeFound = "" Then Exit Function
If bFull Then ' i) search whole strings
IsInArray = (UBound(Filter(Split(Join(arr, ".|") & ".|", "|"), stringToBeFound & ".", , vbTextCompare)) > -1)
Else ' ii) partial match
IsInArray = (UBound(Filter(arr, stringToBeFound, , vbTextCompare)) > -1)
End If
End Function
注意
Public Function IsInArray(stringToBeFound As String, arr As Variant, Optional bFull = False) As Boolean
' Purpose: search strings in 1dim arrays using the Filter function
' i) whole strings (bFull = True) or
' ii) partial matches (bFull = False, default)
' Exclude empty strings "" (Note: if not wanted, comment the next line out!)
If stringToBeFound = "" Then Exit Function
If bFull Then ' i) search whole strings
IsInArray = (UBound(Filter(Split(Join(arr, ".|") & ".|", "|"), stringToBeFound & ".", , vbTextCompare)) > -1)
Else ' ii) partial match
IsInArray = (UBound(Filter(arr, stringToBeFound, , vbTextCompare)) > -1)
End If
End Function
需要注意的是,Filter
函数也将接受空字符串。这就是为什么我在IsInArray函数中也排除了它们;如果您不想这样做,只需将第一行代码注释掉。谢谢,我理解您的意思,但如果您能将它应用到我问题中的函数中,这将非常有帮助。@newguy查看我的编辑#1请查看一下,当stringtobeFound(tbf
)为空时,为什么它与1
匹配?我应该在匹配之前检查字符串是否为空吗?@newguy查看我的编辑#2我有草莓
和草莓-4双关语
以及arr中的许多其他字符串。当我在arr中搜索草莓
时,我只得到草莓
,但当我搜索草莓-4双关语
时,我得到的匹配项草莓
以及草莓-4双关语
。这是唯一的问题。我添加了一个与原始问题“是否有简单的修改?”最接近的替代过滤器
函数,以及一个用于整个字符串搜索和排除空字符串的可选参数。
Public Function IsInArray(stringToBeFound As String, arr As Variant, Optional bFull = False) As Boolean
' Purpose: search strings in 1dim arrays using the Filter function
' i) whole strings (bFull = True) or
' ii) partial matches (bFull = False, default)
' Exclude empty strings "" (Note: if not wanted, comment the next line out!)
If stringToBeFound = "" Then Exit Function
If bFull Then ' i) search whole strings
IsInArray = (UBound(Filter(Split(Join(arr, ".|") & ".|", "|"), stringToBeFound & ".", , vbTextCompare)) > -1)
Else ' ii) partial match
IsInArray = (UBound(Filter(arr, stringToBeFound, , vbTextCompare)) > -1)
End If
End Function