我加载了大约75个包含一行的CSV文件。
For dataRow = 8 To Worksheets("Liste").Range("B65535").End(xlUp).Row
If Dir(FilePath & Worksheets("List").Cells(dataRow, 2) & ".csv") <> "" Then
Open FilePath & Worksheets("List").Cells(dataRow, 2) & ".csv" For Input As #2
Line Input #2, LineFromFile
LineItems = Split(LineFromFile, ";")
itemNumber = 0
For i = 2 To 367
Worksheets("Calendar").Cells(dataRow, i).value = Replace(LineItems(itemNumber), Chr(34), "")
itemNumber = itemNumber + 1
Next
End If
Close #2
Next
问题是,这段代码非常慢。加载此数据大约需要20秒。 (27450个数据集,27,3kb)
有没有办法加快装载速度?我认为我的SSD足够快。
答案 0 :(得分:2)
您可以采取以下两项措施来加快代码速度:
第二项可能会让您获得最快的速度,因为读/写单元非常耗时。试试这段代码:
If Dir(FilePath & Worksheets("List").Cells(datarow, 2) & ".csv") <> "" Then
Open FilePath & Worksheets("List").Cells(datarow, 2) & ".csv" For Input As #2
Line Input #2, LineFromFile
LineFromFile = Replace(LineFromFile, Chr(34), "")
lineitems = Split(LineFromFile, ";")
With Worksheets("Calendar")
.Range(.Cells(datarow, 2), .Cells(datarow, 367)).Value = lineitems
End With
End If