首页 > 解决方案 > 如何在 VBA 中将以下 excel 函数重新创建为自定义函数:=IF(ISBLANK(I2), "late", IF(I2 > H2, "late", "on time"))

问题描述

这是我目前所拥有的,在尝试按如下方式调用函数后返回#NAME 错误:

=开启时间(H2,I2)

这是我的功能-第一次迭代和当前-在从相关线程尝试了一些事情之后:

=IF(ISBLANK(I2), “迟到”, IF(I2 > H2, “迟到”, “准时”))

第一次迭代

Function OnTime(RqstDate, ShipDate)

If IsEmpty(ShipDate) Then

    OnTime = "late"

ElseIf ShipDate > RqstDate Then

    OnTime = "late"

Else

    OnTime = "On Time"

End Function

当前迭代

Function OnTime(RqstDate As Date, ShipDate As Date) As String

    If IsEmpty(ShipDate) Then

        OnTime = "late"

        Exit Function

    ElseIf ShipDate > RqstDate Then

        OnTime = "late"

        Exit Function

    Else

        OnTime = "On Time"

        Exit Function

End Function

直接在 Excel 中键入的函数可以正常工作,但我的 VBA 尝试复制它时没有,我缺少什么才能使其正常运行?

标签: excelvbaexcel-formula

解决方案


你在最后缺少一个End If

Function OnTime(RqstDate As Date, ShipDate As Date) As String
    If IsEmpty(ShipDate) Then
        OnTime = "late"
        Exit Function
    ElseIf ShipDate > RqstDate Then
        OnTime = "late"
        Exit Function
    Else
        OnTime = "On Time"
        Exit Function
    End If 'THIS IS MISSING
End Function

编辑:你的功能在哪里?在您的 Excel 文件中创建一个模块并将您的函数存储在那里。在 ThisWorkbook 中创建的函数在工作表中将不可见。

(这已经过测试并且在工作表中工作正常)


推荐阅读