vba - 为什么 "If Value is Nothing"抛出 "Object Required (Error 424)"

标签 vba if-statement nothing

以下代码抛出 Object Required (Error 424)在 if 语句比较 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在您的函数定义中设置为类型 Variant只有一个 Object可以 SetNothing

关于vba - 为什么 "If Value is Nothing"抛出 "Object Required (Error 424)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38228269/

相关文章:

vb.net - 在 Visual Basic.Net 中将零添加到零之间的区别?

vb.net - "Not ... Is Nothing"与 "... IsNot Nothing"

excel - 如何使用Excel VBA删除Word文档中的特定页面?

vba - Excel宏VBA代码计算一串数字的平均值

excel - 性能问题 - 根据列标题重新排列列

jquery - 返回 Bootstrap 导航选项卡选择的索引

c++ - 两个 for 循环和其中的一个 if 语句

javascript - 如何检查输入字段值是否包含字符?

vb.net - 如何在 VB.Net 2010 中的 double 中使用 null 值?

excel - GetSetting/SaveSetting 和用户权限