将数据输出到Excel表格,使用VBA。

huangapple go评论72阅读模式
英文:

Output array of data to an excel table using VBA

问题

I can help you translate the text, but it seems you've asked for translation in English, and there's no specific text to translate from Chinese to English in your provided content. If you have any other translation needs or if you need assistance with something else, please let me know.

英文:

So I have this array string of data:

> {"202101":8,"202102":null,"202103":null,"202104":null,"202105":null,"202106":null,"202107":null,"202108":null,"202109":null,"202110":null,"202111":null,"202112":null,"202201":null,"202202":null,"202203":null,"202204":null,"202205":null,"202206":null,"202207":null,"202208":null,"202209":null,"202210":null,"202211":null,"202212":null,"202301":null,"202302":null,"202303":null,"202304":null,"rdID":"0003600023OBK00002_ARL","Product
> Code":"0003600023OBK00002","Suffixes":"0003600023OBK00002"},
>
> {"202101":null,"202102":null,"202103":null,"202104":null,"202105":null,"202106":null,"202107":null,"202108":null,"202109":null,"202110":null,"202111":null,"202112":null,"202201":null,"202202":null,"202203":null,"202204":null,"202205":null,"202206":null,"202207":null,"202208":null,"202209":null,"202210":null,"202211":null,"202212":null,"202301":null,"202302":null,"202303":null,"202304":null,"rdID":"0003600023OBK00002_CO","Product
> Code":"0003600023OBK00002","Suffixes":"0003600023OBK00002:}

and I want to output it into a sheet as a table where the first value is the column name and the second value is the row value

so it would look something like this:

将数据输出到Excel表格,使用VBA。

the values may not be the same, but the gist is it would put the values in a table like so.

The number of array values may also be dynamic.

Does anyone know how to do this in VBA? thank you very much.

答案1

得分: 1

从这里导入JsonConverter.bas文件 - https://github.com/VBA-tools/VBA-JSON 到你的工作簿,并添加一个VBA项目引用到Microsoft Scripting runtime库。

英文:

Import the JsonConverter.bas file from here - https://github.com/VBA-tools/VBA-JSON into your workbook, and add a VBA project reference to the Microsoft Scripting runtime library.

Sub tester()
    Dim json, objJson, obj, k, wsOut As Worksheet, m, rw As Range
    
    Set wsOut = ThisWorkbook.Sheets("Output")
    Set rw = wsOut.Rows(2)
    'adding [] so we get a Collection as the output
    json = "[" & ThisWorkbook.Sheets("input").Range("A1").Value & "]"
    
    Set objJson = JsonConverter.ParseJson(json)
    
    For Each obj In objJson     'loop over the collection
        For Each k In obj       'obj=scripting dictionary 
            m = Application.Match(k, wsOut.Rows(1), 0) 'see if the header exists
            If IsError(m) Then  'error if no matching header
                With wsOut.Cells(1, Columns.Count).End(xlToLeft)
                    m = IIf(.Value = "", .Column, .Column + 1)
                End With
                wsOut.Cells(1, m).Value = k 'add the header
            End If
            rw.Cells(m).Value = obj(k)      'populate the value
        Next k
        Set rw = rw.Offset(1)               'next output row
    Next obj

End Sub

huangapple
  • 本文由 发表于 2023年5月10日 14:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定