是Null返回“”

时间:2019-01-14 14:39:10

标签: vba

我有以下代码,但是如果RefDate为空,它将返回#Type!。我可以在一行中添加ifNull =“”

Public Function Status(ByVal RefDate As Date) As String

    Dim Description As String

    Select Case DateDiff("d", Date, RefDate)
        Case > 60
            Description = "In date"
        Case > 0
            Description = "Expiring"
        Case Else
            Description = "Expired"
    End Select

    Status = Description

End Function

如果为Null,则返回“”无内容或为空白

2 个答案:

答案 0 :(得分:0)

尝试一下,我也更改了输入数据类型

Public Function Status(ByVal RefDate As Variant) As String

    Dim Description As String

    If Len(RefDate) > 0 and IsDate(RefDate) Then

        Select Case DateDiff("d", Date, RefDate)
            Case Is > 60
                Status = "In date"
            Case Is > 0
                Status = "Expiring"
            Case Else
                Status = "Expired"
        End Select

    Else

        Status = "No date"

    End If


End Function

答案 1 :(得分:-1)

与@Ryan Wildry进行比较时,尝试类似的方法,但现在将“”明确设置为“说明”

Public Function Status(ByVal RefDate As Date) As String

    Dim Description As String

    If CInt(RefDate) = 0 Then 
        Description = ""
    Else
        Select Case DateDiff("d", Date, RefDate)
            Case Is > 60
                Description = "In date"
            Case Is > 0
                Description = "Expiring"
            Case Else
                Description = "Expired"
        End Select
    End If

    Status = Description

End Function