在Excel VBA中检查对象是否为空

时间:2017-06-01 11:21:46

标签: sql excel vba excel-vba

我无法检查从SQL到Excel的导入值是否为空。在调试模式中,我可以看到指定的值是Null,即不是字符串“Null”。在我的代码示例中尝试过 is Nothing ,但 isEmpty =“Null”

下面的代码中的如果dbList(2)是Nothing Then ,我遇到了麻烦。

如何检查记录集是否为空?

...
Dim CmdSP As New ADODB.Command
CmdSP.CommandType = adCmdText
CmdSP.CommandText = "SELECT FundName, FundId, SRL.Comment FROM XXX SRL ON XXX = XXX ORDER BY FundName ASC"

CmdSP.ActiveConnection = dbConn

Dim dbList As ADODB.Recordset
Set dbList = CmdSP.Execute

Dim row As Integer
row = 1
While Not dbList.EOF
    DataStorage.Range("dsFundsTopLeft")(row, 2) = dbList(0)
    DataStorage.Range("dsFundsTopLeft")(row, 3) = dbList(1)
    If dbList(2) Is Nothing Then
        DataStorage.Range("dsFundsTopLeft")(row, 4) = "No rating"
    Else
        DataStorage.Range("dsFundsTopLeft")(row, 4) = dbList(2)
    End If
    dbList.MoveNext
    row = row + 1
Wend
...

1 个答案:

答案 0 :(得分:2)

尝试功能isNull

If isNull(dbList(2)) Then