VBA-ParseJson(http.responsetext)返回“错误13:类型不匹配”

VBA-ParseJson(http.responsetext)返回“错误13:类型不匹配”,vba,Vba,我正在做一个从API获取数据的项目。 我的测试很好 当我高于14后,它开始出现错误13类型不匹配,并停止 有人能帮我解释一下为什么我会有这个错误吗 如果jsonmetatotal是类型不兼容 我运行了我的maccro很多次,现在它运行到I=103,然后我得到了错误 我将我使用的代码放在下面,以便您可以看到一些改进错误以及我收到的错误原因 提前感谢您的帮助 Sub Test() 'create object to receive datas Dim http As Object,

我正在做一个从API获取数据的项目。 我的测试很好

当我高于14后,它开始出现错误13类型不匹配,并停止

有人能帮我解释一下为什么我会有这个错误吗

如果jsonmetatotal是类型不兼容

我运行了我的maccro很多次,现在它运行到I=103,然后我得到了错误

我将我使用的代码放在下面,以便您可以看到一些改进错误以及我收到的错误原因

提前感谢您的帮助

Sub Test()
    'create object to receive datas
    Dim http As Object, json As Object
    Set http = CreateObject("MSXML2.XMLHTTP")

    'process the data
    i = 1
    k = 2
    While i <= 200
        Dim j As String
        j = i
        Dim url As String
        url = "https://api.worldoftanks.eu/wot/encyclopedia/vehicles/?application_id=demo&tank_id=" & i
        http.Open "GET", url, False
        http.send
        Set json = ParseJson(http.responsetext)

        If json("meta")("total") <> null Then
            ' Tank id
            Sheets(2).Cells(k, 1).Value = json("data")(j)("tank_id")
            ' Tank is premium or Standard
            If json("data")(j)("is_premium") = False Then
                Sheets(2).Cells(k, 2).Value = "Standard"
                Else: Sheets(2).Cells(k, 2).Value = "Premium"
            End If
            ' Tank name
            Sheets(2).Cells(k, 3).Value = json("data")(j)("name")
            ' Tank nation
            Sheets(2).Cells(k, 4).Value = json("data")(j)("nation")
            ' Tank best Radio
            Dim radios As Integer
            radios = 0
            For Each Item In json("data")(j)("radios")
                If Item > radios Then radios = Item Else radios = radios
            Next Item
            Sheets(2).Cells(k, 5).Value = radios
            ' End of Tank Values, next
            i = i + 1
            k = k + 1
        Else: i = i + 1
        End If
    Wend

    MsgBox ("complete")
End Sub
[编辑] 我试过用错误恢复下一个公式

While i <= 100
    j = i
    Dim url As String
    url = myurl & i
    http.Open "GET", url, False
    http.send
    Set response = ParseJson(http.responsetext)

    On Error Resume Next

    If IsNull(response("meta")("total")) Then
    Else:
        ' Tank id
        Sheets(1).Cells(k, 1).Value = response("data")(j)("tank_id")
        ' Tank is premium or Standard
        If response("data")(j)("is_premium") = False Then
            Sheets(1).Cells(k, 2).Value = "Standard"
            Else: Sheets(1).Cells(k, 2).Value = "Premium"
        End If
        ' Tank name
        Sheets(1).Cells(k, 3).Value = response("data")(j)("name")
        ' Tank nation
        Sheets(1).Cells(k, 4).Value = response("data")(j)("nation")
        ' Tank best Radio
        Dim radios As Integer
        radios = 0
        For Each Item In response("data")(j)("radios")
            If Item > radios Then radios = Item Else radios = radios
        Next Item
        Sheets(1).Cells(k, 5).Value = radios
        ' End of Tank Values, next
        k = k + 1
    End If
    i = i + 1
Wend
现在错误没有显示,但是工作表中也没有写入任何内容。 有人能帮我吗?

在某些情况下,如IfVar=Null和IfVarNull,您可能希望计算结果为True的表达式总是False。这是因为任何包含Null的表达式本身都是Null,因此为False

测试非Null的正确方法是

只有当json返回实际的Variant/Null值时,这才有效

如果它只返回长度为零的字符串,您可能应该使用:

If json("meta")("total") <> vbNullString Then
注意:这并不能解释为什么会出现类型不兼容错误,也不能解释为什么每次运行代码时它不会在同一次迭代中崩溃。它只是解释了为什么If语句中没有任何内容正在被处理

If json("meta")("total") <> vbNullString Then
If Not IsEmpty(json("meta")("total")) Then