2016-07-06 57 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

相關問題