2016-07-06 74 views
3

以下代码在if语句处抛出Object Required (Error 424),与Nothing比较,与给定的值无关。为什么?为什么“If Value is Nothing”抛出“Object Required(Error 424)”

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

因为'Value'是'Variant'只有一个'Object'可以设置为' Nothing'。 – Dave

+1

谢谢@Dave。你能否提供这个答案作为答案,所以我可以将其标记为已接受/正确? –

回答

2

因为在你的函数定义Value设置为输入Variant只有一个Object可以SetNothing

+0

我不认为这是真的。 Variant可以被设置为任何东西,包括对象和nothings,但当is'运算符不是对象或Nothing时,会抛出一个类型错误。 – m93a

相关问题