我编写了下面的代码,将单元格的当前值输入到字符串数组中。有52k+行,在调试模式下运行时,如果我在第二次“做”时中断,代码在我按F5之后运行得非常快(秒)。如果没有进入中断,则需要很长时间(6-8分钟)。
有没有人知道为什么会发生这种情况,以及如何解决?
最佳,纳索斯
Do
ProjectIDPub(i, 0) = Cells(3 + i, 13).Value & " - " & Cells(3 + i, 3).Value
ProjectIDPub(i, 1) = Cells(3 + i, 7).Value
i = i + 1
Loop Until i = numberRowsAXProjectsSheetPub
i = 0
Do
Cells(3 + i, 14).Value = ProjectIDPub(i, 0)
Cells(3 + i, 15).Value = ProjectIDPub(i, 1)
i = i + 1
Loop Until i = numberRowsAXProjectsSheetPub
i = 0发布于 2016-09-29 12:18:49
假设numberRowsAXProjectsSheetPub是数组ProjectIDPub中的行数,则整个第二个循环可以替换为
Range(Cells(3,14),Cells(3 + numberRowsAXProjectsSheetPub - 1,15)).Value = ProjectIDPub在单个赋值中将数组传输到工作表几乎总是比在循环中快得多。
发布于 2016-09-29 12:06:39
您试过在禁用屏幕更新的情况下运行这些循环吗?
Application.ScreenUpdating = False
Do
ProjectIDPub(i, 0) = Cells(3 + i, 13).Value & " - " & Cells(3 + i, 3).Value
ProjectIDPub(i, 1) = Cells(3 + i, 7).Value
i = i + 1
Loop Until i = numberRowsAXProjectsSheetPub
i = 0
Do
Cells(3 + i, 14).Value = ProjectIDPub(i, 0)
Cells(3 + i, 15).Value = ProjectIDPub(i, 1)
i = i + 1
Loop Until i = numberRowsAXProjectsSheetPub
i = 0
Application.ScreenUpdating = True正如@ali所问的,为什么需要运行相同的循环两次?如果第二个循环访问了一些需要由第一个循环创建的数据,那么您应该能够在需要时为i修改一些偏移量,然后只运行一次。
https://stackoverflow.com/questions/39769391
复制相似问题