英文:
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:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论