我正在做一个从API中提取数据并通过相关列解析的项目,第一个项目运行得很好,但它的优化速度非常慢。
所以我想把它转换成数组以便快速处理,但是Run-time error 9
下标超出了范围
我们将非常感谢您的帮助来解决这个问题。
第一个优化缓慢的代码。
Dim json As Object
Dim timeEntry As Object
Dim ti As Object
Dim lastRow As Long
Dim myValue As String
Set json = JsonConverter.ParseJson(Data)
i = 2
lastRow = Sheet2.Range("A1").End(xlUp).Row
For Each timeEntry In json("timeentries")
With Sheet2.Cells(i, 1)
.Value = timeEntry("projectName")
.Offset(0, 4).Value = timeEntry("taskName")
.Offset(0, 8).Value = timeEntry("description")
.Offset(0, 9).Value = timeEntry("clientName")
End With
Set ti = timeEntry("timeInterval")
With Sheet2.Cells(i, 1)
.Offset(0, 10).Value = ti("start")
.Offset(0, 6).Value = ti("duration")
End With
i = i + 1
Next timeEntry
第二个代码使用数组并获得错误
Dim json As Object
Dim timeEntry As Object
Dim ti As Object
Dim lastRow As Long
Dim myValue As String
Set json = JsonConverter.ParseJson(Data)
i = 2
lastRow = Sheet2.Range("A1").End(xlUp).Row
Dim dataArray() As Variant
ReDim dataArray(1 To lastRow, 1 To 12)
For Each timeEntry In json("timeentries")
dataArray(i, 1) = timeEntry("projectName")
dataArray(i, 5) = timeEntry("taskName")
dataArray(i, 9) = timeEntry("description")
dataArray(i, 10) = timeEntry("clientName")
Set ti = timeEntry("timeInterval")
dataArray(i, 11) = ti("start")
dataArray(i, 7) = ti("duration")
i = i + 1
Next timeEntry
Sheet2.Range("A2").Resize(lastRow, 12).Value = dataArray
1条答案
按热度按时间8xiog9wr1#
将数组大小调整为条目数