Excel UDF调用两次,每次返回不同的值

问题描述

我正在尝试在表中的给定日期查找值。表格标题是月份,下面的项目是要查找的值。我正在使用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

任何帮助将不胜感激!

解决方法

由于我没有收到答案,因此我重新编码了例程,以使用标准Excel执行匹配并使用VBA读取该例程。这按预期工作。我担心第一次调用后调用例程会得到错误的答案并计算出错误的结果。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...