Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba “为什么?”;如果值为“无”;扔;所需对象(错误424)“;_Vba_If Statement_Nothing - Fatal编程技术网

Vba “为什么?”;如果值为“无”;扔;所需对象(错误424)“;

Vba “为什么?”;如果值为“无”;扔;所需对象(错误424)“;,vba,if-statement,nothing,Vba,If Statement,Nothing,以下代码在if语句中抛出所需对象(错误424),与Nothing进行比较,这与给定的值无关。为什么? Public Function SqlizeCellValue(ByVal Value As Variant) As Variant If Value Is Nothing Then SqlizeCellValue = Nothing ElseIf Value = Null Then SqlizeCellValue = Null ElseI

以下代码在if语句中抛出
所需对象(错误424)
,与
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

因为函数定义中的
Value
设置为type
Variant
,只有
对象
可以
设置为
Nothing

,因为
Value
Variant
并且只有
对象
可以设置为
Nothing
。谢谢@Dave。你能提供这个答案吗,这样我就可以把它标记为接受/正确?我认为这不是真的
Variant
可以设置为任何值,包括objects和Nothing,但是
is
运算符在不是object或
Nothing
时抛出类型错误。