VBA - 如何加快带日期的循环?
VBA - How to speed up Loop with dates?
正在寻找有关如何优化此代码以 运行 更快的想法。当前代码有效,但 table 太大,性能速度很慢。
总体逻辑:确定一个月的天数。循环遍历 table(wsUploadTable) 并在满足条件时递增值。循环到一个月中的第二天并重复。
例如:10/1/2016,循环遍历 table 以进行日期匹配和增量值。下一个日期,10/2/2016 循环 table 匹配...直到 2016 年 10 月 31 日的最后一天,循环 table,找到匹配并增加值
'Determine DaysinMonth and assign DaysinMonth_Distro value
DaysInMonth = DateSerial(dtTrickle_Year, dtTrickle_Month + 1, 1) - _
DateSerial(dtTrickle_Year, dtTrickle_Month, 1)
DoM_Distro = 1 / DaysInMonth
ReDim Days(1 To DaysInMonth)
For i = 1 To DaysInMonth
Days(i) = DateSerial(dtTrickle_Year, dtTrickle_Month, i)
'Loop Upload Table and increment cell value if condition is met
With wsUploadTable
lngER_PrimaryID = .Cells(1048576, 2).End(xlUp).Row
For intPrimaryID = 2 To lngER_PrimaryID
'store current cell value
dblLeadsValue = .Cells(intPrimaryID, col_Upload_Leads)
'match UploadTable row on user input and increment new value
If.Cells(intPrimaryID, 3).Value = Days(i) Then
.Cells(intPrimaryID, 11).Value = dblLeadsValue + (x * x * DoM_Distro)
End If
Next 'Next PrimaryID
End With
Next i
将table列存储到一个数组中(1次读操作),遍历数组并将计算存储在数组中,将结果值写回table(1次写操作).这将比 table.
中每一行的读取和写入快得多
正在寻找有关如何优化此代码以 运行 更快的想法。当前代码有效,但 table 太大,性能速度很慢。
总体逻辑:确定一个月的天数。循环遍历 table(wsUploadTable) 并在满足条件时递增值。循环到一个月中的第二天并重复。
例如:10/1/2016,循环遍历 table 以进行日期匹配和增量值。下一个日期,10/2/2016 循环 table 匹配...直到 2016 年 10 月 31 日的最后一天,循环 table,找到匹配并增加值
'Determine DaysinMonth and assign DaysinMonth_Distro value
DaysInMonth = DateSerial(dtTrickle_Year, dtTrickle_Month + 1, 1) - _
DateSerial(dtTrickle_Year, dtTrickle_Month, 1)
DoM_Distro = 1 / DaysInMonth
ReDim Days(1 To DaysInMonth)
For i = 1 To DaysInMonth
Days(i) = DateSerial(dtTrickle_Year, dtTrickle_Month, i)
'Loop Upload Table and increment cell value if condition is met
With wsUploadTable
lngER_PrimaryID = .Cells(1048576, 2).End(xlUp).Row
For intPrimaryID = 2 To lngER_PrimaryID
'store current cell value
dblLeadsValue = .Cells(intPrimaryID, col_Upload_Leads)
'match UploadTable row on user input and increment new value
If.Cells(intPrimaryID, 3).Value = Days(i) Then
.Cells(intPrimaryID, 11).Value = dblLeadsValue + (x * x * DoM_Distro)
End If
Next 'Next PrimaryID
End With
Next i
将table列存储到一个数组中(1次读操作),遍历数组并将计算存储在数组中,将结果值写回table(1次写操作).这将比 table.
中每一行的读取和写入快得多