我正在编写一个代码,该代码将搜索值在范围内出现的特定位置。我的方法是在基于输入的预定义搜索范围上使用Find方法进行循环,并将每个查找结果添加到集合中。但是,当我在另一个子程序中调用UDF时,代码将按预期工作,但是当放入Excel中时,它会返回0,且输入相同。我在Excel中通过UDF尝试了F8,显然,在Excel中使用时,“查找方法”不返回任何内容,而当另一个子对象调用UDF时,它可以正常工作。我已包含以下代码。任何投入将不胜感激。
Public Function CROSSEDVOLUME(dt As Variant, datarange As Range, date_col As Long, real_vol_col As Long, abs_vol_col As Long) As Double
Dim filtered_arr() As Variant
Dim i As Long, coll As Collection
Dim ori_col As Long, ori_row As Long
Dim find_res As Range, search_range As Range, first_occur As String
ori_row = datarange.Cells(1, 1).Row
ori_col = datarange.Cells(1, 1).column
Set coll = New Collection
'Setting search range
Set search_range = Range(datarange.Cells(1, date_col),
datarange.Cells(datarange.Rows.count, date_col))
If search_range.Cells(1, 1).Value = dt Then
coll.Add 1
first_occur = search_range.Cells(1, 1).Address
End If
Set find_res = search_range.Find(dt, search_range.Cells(1, 1), xlValues, xlWhole, xlByRows, xlNext, False, False)
'Code stucks here
If find_res Is Nothing Then
CROSSEDVOLUME = 0
Exit Function
Else
If find_res.Address <> search_range.Cells(1, 1).Address Then
first_occur = IIf(first_occur = "", find_res.Address, first_occur)
coll.Add find_res.Row - ori_row + 1
Set find_res = search_range.Find(dt, find_res, xlValues, xlWhole, xlByRows, xlNext, False, False)
Do While Not find_res Is Nothing And find_res.Address <> first_occur
coll.Add find_res.Row - ori_row + 1
Set find_res = search_range.Find(dt, find_res, xlValues, xlWhole, xlByRows, xlNext, False, False)
Loop
End If
End If
CROSSEDVOLUME = coll.count
End Function