什么比较范围操作返回

时间:2015-09-28 15:21:31

标签: excel vba excel-vba search

我正在尝试编写UDF,在给定的文本中搜索数组中给出的目标。

为简单起见,我尝试使用application.findapplication.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_result(i)则不返回任何内容。

如何迭代search_result

到目前为止,只有当一个项目匹配时,我的功能才有效,这非常糟糕。

完整代码如下:

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

1 个答案:

答案 0 :(得分:0)

  

但是如果我使用search_result(i)它什么都不返回。   我怎样才能遍历search_result?

search_result是一个2D数组。要遍历它,您可以使用

找到上限
?UBound(search_result)

然后简单地循环它

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