Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA编译错误:未定义变量(JSON)_Json_Vba - Fatal编程技术网

VBA编译错误:未定义变量(JSON)

VBA编译错误:未定义变量(JSON),json,vba,Json,Vba,我正在运行测试代码,并根据需要进行编辑。当我尝试运行它时,我得到一个JSON突出显示的错误编译错误:变量未定义。代码取自本文:因此它应该是有效代码。我已经导入了JsonConverter模块,并添加了Microsoft脚本运行时参考 编辑:我使用的是VBA-JSON v2.3.1,也更新了VBA-UTC v.1.0.6 Option Explicit Sub Test() Dim sJSONString As String Dim vJSON Dim sState A

我正在运行测试代码,并根据需要进行编辑。当我尝试运行它时,我得到一个JSON突出显示的错误
编译错误:变量未定义
。代码取自本文:因此它应该是有效代码。我已经导入了
JsonConverter
模块,并添加了
Microsoft脚本运行时
参考

编辑:我使用的是
VBA-JSON v2.3.1
,也更新了
VBA-UTC v.1.0.6

Option Explicit

Sub Test()

    Dim sJSONString As String
    Dim vJSON
    Dim sState As String
    Dim aData()
    Dim aHeader()
    Dim vResult
    Dim sName
    Dim authKey As String
    authKey = "my_auth_key"

    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://my_site_url", True
        .SetRequestHeader "Authorization", "Bearer " & authKey
        .send
        Do Until .readyState = 4: DoEvents: Loop
        sJSONString = .responseText
    End With
    JSON.Parse sJSONString, vJSON, sState
    If sState = "Error" Then
        MsgBox "Invalid JSON"
        End
    End If
    vResult = vJSON("data")
    JSON.ToArray vResult, aData, aHeader
    With ThisWorkbook.Sheets(1)
        .Cells.Delete
        .Cells.WrapText = False
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    vJSON.Remove "data"
    Application.DisplayAlerts = False
    With ThisWorkbook.Sheets
        Do Until .Count = 1
            .Item(.Count).Delete
        Loop
    End With
    Application.DisplayAlerts = True
    For Each sName In vJSON
        If IsArray(vJSON(sName)) Or IsObject(vJSON(sName)) Then
            JSON.ToArray vJSON(sName), aData, aHeader
            With ThisWorkbook.Sheets.Add 
                OutputArray .Cells(1, 1), aHeader
                Output2DArray .Cells(2, 1), aData
                .Columns.AutoFit
            End With
            vJSON.Remove sName
        End If
    Next
    JSON.ToArray vJSON, aData, aHeader
    With ThisWorkbook.Sheets.Add 
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    JSON.Parse sJSONString, vJSON, sState
    JSON.Flatten vJSON, vResult
    JSON.ToArray vResult, aData, aHeader
    With ThisWorkbook.Sheets.Add 
        OutputArray .Cells(1, 1), aHeader
        Output2DArray .Cells(2, 1), aData
        .Columns.AutoFit
    End With
    MsgBox "Completed"

End Sub

Sub OutputArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize(1, UBound(aCells) - LBound(aCells) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

Sub Output2DArray(oDstRng As Range, aCells As Variant)

    With oDstRng
        .Parent.Select
        With .Resize( _
                UBound(aCells, 1) - LBound(aCells, 1) + 1, _
                UBound(aCells, 2) - LBound(aCells, 2) + 1)
            .NumberFormat = "@"
            .Value = aCells
        End With
    End With

End Sub

您的代码中的
JSON
是什么?导入JSON.bas模块时,是否确保在项目中将其命名为
JSON
?您应该只需要一个JSON解析器。你的代码是为VBA JSON解析器编写的,所以为什么不直接使用它,并确保按照上面的注释正确导入呢。我按照指示导入了
JsonConverter
而不是JSON.bas。出于某种原因,我想他们也会这么做。最后一件要考虑的事情是如何从这个拉取的数据(例如,
worker\u id
first\u name
等)生成第一行自动显示为可过滤的。您的代码中的
JSON
是什么?导入JSON.bas模块时,是否确保在项目中将其命名为
JSON
?您应该只需要一个JSON解析器。你的代码是为VBA JSON解析器编写的,所以为什么不直接使用它,并确保按照上面的注释正确导入呢。我按照指示导入了
JsonConverter
而不是JSON.bas。出于某种原因,我想他们也会这么做。最后一件要考虑的事情是如何从这个拉取的数据(例如,
worker\u id
first\u name
等)生成第一行以自动显示为可过滤。