比较范围操作返回什么?

我试图编写UDF,在给定的文本中search数组中给定的目标。

为了简单起见,我尝试使用application.findapplication.matchapplication.iferror等应用程序function。

我坚持这段代码:

 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表示作为常规Find的第9个项目“find的项目” )会返回

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

但是,如果我使用search_result(i)它什么都不返回。

我怎样才能遍历search_result

到目前为止,我的function只有当只有一个项目匹配,这是非常糟糕的。

完整的代码如下所示:

 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)它什么都不返回。 我怎样才能遍历search_result?

search_result是一个2D数组。 通过它循环,你可以find使用的上限

 ?UBound(search_result) 

然后简单地通过它循环

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