Vba “为什么?”;如果值为“无”;扔;所需对象(错误424)“;
以下代码在if语句中抛出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
所需对象(错误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
设置为typeVariant
,只有对象可以设置为Nothing
,因为Value
是Variant
并且只有对象可以设置为Nothing
。谢谢@Dave。你能提供这个答案吗,这样我就可以把它标记为接受/正确?我认为这不是真的Variant
可以设置为任何值,包括objects和Nothing,但是is
运算符在不是object或Nothing
时抛出类型错误。