【发布时间】:2019-01-31 17:33:59
【问题描述】:
我正在尝试使用字典将与唯一 ID 组合关联的总余额移动到另一张表。循环可能需要在数万行上运行,即使在 900 行上,该过程也需要大约 30 秒。
我的代码可以处理(多个)字典和循环,但速度很慢。我想知道是否有办法优化循环(可能通过使用数组?虽然我对它们非常缺乏经验)。
我尝试为 I=lbound 设置一个数组循环到 ubound,但我离让它工作还差得很远(代码一团糟)。下面是一个 sn-p 代码和我正在尝试优化的循环之一。稍后还有其他 4 个循环,但现在我只想优化一个。
'declare start/end rows
Dim StartRowPeriod As Long
StartRowPeriod = 7
Dim LastRowPeriod As Long
LastRowPeriod = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
'more dims for total bal
Dim HardCopyID As String
Dim Old_Balance As Double
Dim New_Balance As Double
Dim Updated_Balance As Double
Application.ScreenUpdating = False
'RUNNING THE DICTIONARY (ADDING THE TOTAL VALUES TO THE UNIQUE IDS)
For I = StartRowPeriod To LastRowPeriod
HardCopyID = Cells(I, 11).Value
If HardCopyID = "" Then
Exit For
ElseIf HardCopy_Dictionary.Exists(HardCopyID) Then
Old_Balance = HardCopy_Dictionary(HardCopyID)
New_Balance = Cells(I, 10).Value
Updated_Balance = Old_Balance + New_Balance
HardCopy_Dictionary(HardCopyID) = Updated_Balance
Else
HardCopy_Dictionary(HardCopyID) = Cells(I, 10).Value
End If
Next I
【问题讨论】:
标签: arrays excel vba dictionary