Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Ubound(Filter())函数为部分字符串匹配返回true_Vba_Excel_Filter - Fatal编程技术网

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