【发布时间】:2021-04-06 21:02:54
【问题描述】:
我试图根据映射条目(索引 i)循环近 700 行,循环 i 的每次迭代都需要很长时间(长时间循环 j)。可能是什么原因?我几乎每天都写宏,从来没有遇到过这个问题。 我也尝试只保留循环 j 中的第一行,但一直遇到这个问题
业务需求:是提取工作表中的所有列,其中列 C 包含映射表中的值(即满足此条件 PacketWork = Sheets(sheetname).Cells(j, Column3).Value 其中第 3 列是“C”
For i = 2 To lastRowLevelMapping
PacketWork = Sheets("Mapping").Cells(i, "A").Value
For j = 2 To LastRowSheet
If (PacketWork = Sheets(sheetname).Cells(j, Column3).Value) Then
Sheets("Source").Cells(writer_counter, "A").Value = Sheets(sheetname).Cells(j, Column1).Value
Sheets("Source").Cells(writer_counter, "B").Value = Sheets(sheetname).Cells(j, Column2).Value
Sheets("Source").Cells(writer_counter, "C").Value = Sheets(sheetname).Cells(j, Column3).Value
Sheets("Source").Cells(writer_counter, "D").Value = Sheets(sheetname).Cells(j, Column4).Value
data = Sheets(sheetname).Cells(j, Column5).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "E").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "E").Value = data
End If
data = Sheets(sheetname).Cells(j, Column6).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "F").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "F").Value = data
End If
data = Sheets(sheetname).Cells(j, Column7).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "G").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "G").Value = data
End If
data = Sheets(sheetname).Cells(j, Column8).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "H").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "H").Value = data
End If
data = Sheets(sheetname).Cells(j, Column9).Value
If (IsNumeric(data)) Then
Sheets("Source").Cells(writer_counter, "I").Value = Round(data, 2)
Else
Sheets("Source").Cells(writer_counter, "I").Value = data
End If
If (IsDate(Sheets(sheetname).Cells(j, Column10).Value)) Then
Sheets("Source").Cells(writer_counter, "P").Value = Format(Sheets(sheetname).Cells(j, Column10).Value, "dd/mm/yyyy")
Else
Sheets("Source").Cells(writer_counter, "P").Value = Sheets(sheetname).Cells(j, Column10).Value
End If
Sheets("Source").Cells(writer_counter, "P").NumberFormat = "dd/mm/yyyy"
If (Sheets("Source").Cells(writer_counter, "E").Value <> 0) Then
Sheets("Source").Cells(writer_counter, "L").Value = Round((Sheets("Source").Cells(writer_counter, "F").Value / Sheets("Source").Cells(writer_counter, "E").Value) * 100, 2)
Sheets("Source").Cells(writer_counter, "M").Value = Round((Sheets("Source").Cells(writer_counter, "G").Value / Sheets("Source").Cells(writer_counter, "E").Value) * 100, 2)
Else
Sheets("Source").Cells(writer_counter, "L").Value = 0
Sheets("Source").Cells(writer_counter, "M").Value = 0
End If
Sheets("Source").Cells(writer_counter, "L").NumberFormat = "0.00"
Sheets("Source").Cells(writer_counter, "M").NumberFormat = "0.00"
If (Sheets("Source").Cells(writer_counter, "L").Value > 100) Then
Sheets("Source").Cells(writer_counter, "L").Value = 100
End If
If (Sheets("Source").Cells(writer_counter, "M").Value > 100) Then
Sheets("Source").Cells(writer_counter, "M").Value = 100
End If
If (Contains(Mapping2Collection, Sheets(sheetname).Cells(j, Column4).Value)) Then
Sheets("Source").Cells(writer_counter, "N").Value = Mapping2Collection(Sheets(sheetname).Cells(j, Column4).Value)
End If
Sheets("Source").Cells(writer_counter, "K").Value = Sheets("Mapping").Cells(i, "D").Value
Sheets("Source").Cells(writer_counter, "J").Value = Sheets("Mapping").Cells(i, "C").Value
Sheets("Source").Cells(writer_counter, "O").Value = Sheets("Mapping").Cells(i, "B").Value
writer_counter = writer_counter + 1
End If
Next j
Next i
【问题讨论】:
-
嵌套循环在这里是一种代码味道。考虑使用
Range.Find或Application.Match而不是循环查找匹配项。还可以考虑将数据加载到Variant数组中并在内存中进行所有处理,而不是重复引用工作表。 -
@BigBen 谢谢,我会试试的
-
在开头设置
Application.ScreenUpdating = False并返回到Application.ScreenUpdating = True也可以加快速度。 -
你可以试试this
-
@Storax 我已经使用了这个,但没有重大变化