为什么“If Value is Nothing”抛出“Object Required(Error 424)”

时间:2016-07-06 15:49:33

标签: vba if-statement nothing

以下代码将Object Required (Error 424)抛出if语句,与Nothing进行比较,与给定的值无关。为什么呢?

Public Function SqlizeCellValue(ByVal Value As Variant) As Variant
    If Value Is Nothing Then
        SqlizeCellValue = Nothing
    ElseIf Value = Null Then
        SqlizeCellValue = Null
    ElseIf Value = "" Then
        SqlizeCellValue = Null
    ElseIf Value = "0" Then
        SqlizeCellValue = Null
    ElseIf Value = "---" Then
        SqlizeCellValue = Null
    ElseIf LCase(Value) = "n.c." Then
        SqlizeCellValue = Null
    Else
        SqlizeCellValue = Value
    End If
End Function
Public Sub TestSqlizeCellValue()
    Debug.Assert SqlizeCellValue("") Is Null
    Debug.Assert SqlizeCellValue("0") Is Null
    Debug.Assert SqlizeCellValue("---") Is Null
    Debug.Assert SqlizeCellValue(Nothing) Is Nothing
    Debug.Assert SqlizeCellValue(Null) Is Null
End Sub

1 个答案:

答案 0 :(得分:2)

因为您的函数定义中的Value设置为Variant类型,而Object只能SetNothing