【发布时间】:2020-08-18 19:15:53
【问题描述】:
我试图在表格中查找给定日期的值。表标题是月份,下面的项目是要查找的值。我正在使用 Application.Match 函数来查找其中包含日期的列。奇怪的是该函数总是被调用两次。第一次通过,col 返回“空”。第二次通过它返回正确的列,但我看不出对 Match 函数的调用有任何区别。函数如下:
Function find_needed(ByVal rng As Range, ByVal project As String, _
ByVal mon As Date, ByVal eng_type As Integer) As String
Dim col As Variant
Dim dd As Double
On Error GoTo duh
find_needed = 0
dd = CDbl(mon)
Debug.Print (rng.Address)
Debug.Print (dd)
col = Application.Match(dd, rng, 0)
If IsError(col) Or col = xlErrNA Then
MsgBox "Error"
Else
If col <> 0 Then
find_needed = Worksheets(project).Cells(eng_type + 3, col).Value
End If
End If
GoTo miss
duh:
MsgBox "Error:" & Err.Description
miss:
End Function
任何帮助将不胜感激!
【问题讨论】:
-
“奇怪的是函数总是被调用两次” - 你怎么知道它被调用了两次?
-
你能附上一张图片来显示布局,可以帮助测试 udf
-
我在工作表单元格中尝试了您的 UDF,它工作得很好。您是在单元格中使用它还是在子级中使用它??
-
find_needed = Worksheets(project).Cells(eng_type + 3, col).Value如果您打开了多个工作簿并且 UDF 不在活动工作簿中,它将尝试访问活动工作簿中的命名工作表。我肯定会标记Thisworkbook。 -
我认为 UDF 被多次调用并不罕见,这可能是由于 Excel 如何计算出计算树。见 - decisionmodels.com/calcsecretsj.htm
标签: excel vba match user-defined-functions