【问题标题】:Why does "If Value is Nothing" throw "Object Required (Error 424)"为什么“如果值没有”抛出“需要对象(错误 424)”
【发布时间】:2016-11-08 18:09:40
【问题描述】:

以下代码在 if 语句中抛出 Object Required (Error 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

【问题讨论】:

  • 因为ValueVariant,只有Object可以设置为Nothing
  • 谢谢@Dave。请您提供这个作为答案,以便我可以将其标记为已接受/正确?

标签: vba if-statement nothing


【解决方案1】:

因为您的函数定义中的Value 设置为类型Variant,并且只有Object 可以是SetNothing

【讨论】:

  • 我认为这不是真的。 Variant 可以设置为任何东西,包括对象和虚无,但is 运算符在它既不是对象也不是Nothing 时会引发类型错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多