Vba 比较范围操作返回什么

Vba 比较范围操作返回什么,vba,excel,search,Vba,Excel,Search,我试图编写UDF,它在给定文本中搜索数组中给定的目标 为了简化操作,我尝试使用应用程序函数,如application.find和application.match和application.iferror等 我坚持使用这段代码: Function SEARCHARRAY(find_items As Range, within_text As Range) As Variant Dim search_result As Variant search_result = Applica

我试图编写UDF,它在给定文本中搜索数组中给定的目标

为了简化操作,我尝试使用应用程序函数,如
application.find
application.match
application.iferror

我坚持使用这段代码:

Function SEARCHARRAY(find_items As Range, within_text As Range) As Variant
    Dim search_result As Variant

    search_result = Application.IfError(Application.Find(find_items, within_text), 0) //this should return in application sothing like {0;0;9}, that represent range of items, 0 relate to items not found and 9 i "item found on the 9th position" as regular Find() would return 
` application.find返回一个数组,该数组在工作表上看起来是{0,0,9},表示一系列项,其中0与未找到的项相关,9是“在第9个位置找到的项”,就像常规find()返回的一样

在后面的代码中,我需要找出这个{#、#、#}数组中有多少匹配项

但是如果我使用
search\u result(I)
它不会返回任何结果

如何遍历
搜索结果

到目前为止,我的函数只在只有一个项匹配时工作,这是非常糟糕的

完整代码如下所示:

Function SEARCHARRAY(find_items As Range, within_text As Range) As Variant
Dim search_result, position As Variant

search_result = Application.IfError(Application.Find(find_items, within_text), 0)

If (Application.Sum(search_result) = 0) Then
    matched_item = "no match"
Else
    position = Application.Match(Application.Sum(search_result), search_result, 0)
    matched_item = Application.Index(find_items, position)
End If
SEARCHARRAY = matched_item
End Function
但是如果我使用search_result(I),它将不返回任何内容。 如何迭代搜索结果

搜索结果
是一个二维数组。要循环遍历它,可以使用

?UBound(search_result)
然后简单地循环通过它

For i = LBound(search_result) To UBound(search_result)
    Debug.Print search_result(i, 1)
Next i
但是如果我使用search_result(I),它将不返回任何内容。 如何迭代搜索结果

搜索结果
是一个二维数组。要循环遍历它,可以使用

?UBound(search_result)
然后简单地循环通过它

For i = LBound(search_result) To UBound(search_result)
    Debug.Print search_result(i, 1)
Next i