【发布时间】:2016-11-04 20:06:05
【问题描述】:
我正在尝试为 QuickBooks 导出进行一些数据格式化,但一步非常慢。我有一个名为“输出”的工作表,其中每个条目都以所需的格式排列,但我只希望将完全填充的条目用于另一个名为“地图”的工作表上。
到目前为止,一切都是用公式完成的,这部分工作正常。我编写了一个小脚本来遍历所有条目并将相关信息从“输出”拉到五个不同的数组中。然后它循环回这些数组并填充“地图”中相应列中的单元格。
我的脚本快速填充数组,但填充单元格需要很长时间。我使用 for 循环遍历数组,每次迭代大约需要 3 秒,当您处理数千个条目时,这是一个很长的时间。
Sub Prettify()
Dim numbers()
Dim catagories()
Dim classes()
Dim subclasses()
Dim values()
Dim count As Integer
count = 2
' The upper bounds of the loop is a calculation of the number of entries we will access
For i = 2 To (Sheets("Data").Cells(7, 8).Value * Sheets("Data").Cells(4, 3).Value + 2)
If (Sheets("Output").Cells(i, 1).Value = "") Then
' Do Nothing
Else
ReDim Preserve numbers(count)
ReDim Preserve catagories(count)
ReDim Preserve classes(count)
ReDim Preserve subclasses(count)
ReDim Preserve values(count)
count = count + 1
numbers(count - 2) = Val((Sheets("Output").Cells(i, 1).Value))
catagories(count - 2) = Sheets("Output").Cells(i, 2).Value
If (Sheets("Output").Cells(i, 3).Value = 0) Then
classes(count - 2) = Sheets("Output").Cells(i, 4).Value
subclasses(count - 2) = ""
Else
classes(count - 2) = Sheets("Output").Cells(i, 3).Value
subclasses(count - 2) = Sheets("Output").Cells(i, 4).Value
End If
values(count - 2) = Sheets("Output").Cells(i, 5).Value
End If
Next
MsgBox (numbers(0))
MsgBox (catagories(0))
Sheets("Map").Activate
' This next part is slow
For j = 2 To count
Sheets("Map").Cells(j, 1).Value = numbers(j - 2)
Sheets("Map").Cells(j, 2).Value = catagories(j - 2)
Sheets("Map").Cells(j, 3).Value = classes(j - 2)
Sheets("Map").Cells(j, 4).Value = subclasses(j - 2)
Sheets("Map").Cells(j, 5).Value = values(j - 2)
Next
End Sub
大约三年前,我在一篇帖子中遇到过类似的问题,但他们使用的修复程序不适用于我的示例。我使用消息框在不同点测试了代码,最后一个 for 循环中的五个分配步骤中的每一个都同样慢。想法?
【问题讨论】:
-
你能链接到你提到的问题吗?我们在谈论多少价值观?
-
当你使用
Sheets("Map").Range("A2:A"& count).Value = numbers而不是循环时会发生什么? -
不幸的是,当我这样做时,什么都没有填充。
-
感谢 DAXaholic 的编辑!