Write large collection object (parsed from json) to excel range

前端 未结 3 1989
灰色年华
灰色年华 2020-12-06 18:20

I am trying to convert a json api to excel table. I tried different parsing methods but currently using VBA-JSON (similar to VB-JSON but faster parsing). So far I got it to

3条回答
  •  眼角桃花
    2020-12-06 19:14

    It is faster to write all of the values at once then to do it cell by cell. Also you may have secondary event firing so disabling events may help with performance. If performance is still poor with the below code the problem is with the performance of JsonConverter.

    Dim ItemCount As Integer
    Dim items() As Variant
    
    Function httpresp(URL As String) As String
        Dim x As Object: Set x = CreateObject("MSXML2.XMLHTTP")
        x.Open "GET", URL, False
        x.send
        httpresp = x.responseText
    End Function
    
    Private Sub btnLoad_Click()
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
        Application.EnableEvents = False
    
        Dim URL As String: URL = "https://www.gw2shinies.com/api/json/item/tp"
        Dim DecJSON As Object: Set DecJSON = JsonConverter.ParseJson(httpresp(URL))
        ItemCount = DecJSON.Count
        ReDim items(1 To ItemCount, 1 To 1)
        Range("A2:S25000").Clear                'clear range
        Dim test As Variant
        For i = 1 To ItemCount
            items(i, 1) = DecJSON(i)("item_id")
            'Cells(i + 1, 1).Value = DecJSON(i)("item_id")
        Next i
        Range(Range("A2"), Range("A2").Offset(ItemCount)).Value = items
    
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        Application.EnableEvents = True
    End Sub
    

提交回复
热议问题