缓慢的嵌套 For 循环
Slow, nested For loop
显示的代码是一个子例程,它比较两个数组中的数据,以查找出现在一个列表中但未出现在另一个列表中的帐号。如果它发现缺少一个,它会将帐户信息添加到 Sheet。在平常的一天,它会从 2,500 - 4,000 条记录中找到 10-20 个丢失的帐户,具体取决于我们一个月的时间。处理 i 循环需要 15-20 分钟。与 Sheet 的互动很少。我不知道如何改进代码以使其 运行 更快。赞赏建议。
For i = 1 To TempCount
If i Mod 5 = 0 Then DoEvents
FoundMatch = 0
percent = i / TempCount * 100
Application.StatusBar = "Checking for missing accounts. Processing row " & i & " of " & TempCount & " - " & percent & "%"
For y = 1 To OppListRow
If Saved_User_Input(i, 3) = AccountList(y, 3) Then
FoundMatch = 1
End If
Next
If FoundMatch = 0 Then
n = n + 1
Opportunities.Cells(n, 2) = Saved_User_Input(i, 1)
Opportunities.Cells(n, 3) = Saved_User_Input(i, 2)
Opportunities.Cells(n, 4) = Saved_User_Input(i, 3)
Opportunities.Cells(n, 5) = Saved_User_Input(i, 4)
Opportunities.Cells(n, 6) = Saved_User_Input(i, 5)
Opportunities.Cells(n, 7) = Saved_User_Input(i, 6)
Opportunities.Cells(n, 8) = Saved_User_Input(i, 7)
Opportunities.Cells(n, 9) = Saved_User_Input(i, 8)
Opportunities.Cells(n, 10) = Saved_User_Input(i, 9)
Opportunities.Cells(n, 11) = Saved_User_Input(i, 10)
Opportunities.Cells(n, 12) = Saved_User_Input(i, 11)
Opportunities.Cells(n, 13) = Saved_User_Input(i, 12)
Opportunities.Cells(n, 14) = Saved_User_Input(i, 13)
Opportunities.Cells(n, 15) = Saved_User_Input(i, 14)
Opportunities.Cells(n, 16) = Saved_User_Input(i, 15)
End If
Next
将所有你的Opportunities.Cells行替换为这个单个一个
Opportunities.Cells(n, 2).Resize(1, 15).Value2 = WorksheetFunction.Index(Saved_User_Input, i, 0)
编辑(消除一个循环)
替换下面的循环
For y = 1 To OppListRow
If Saved_User_Input(i, 3) = AccountList(y, 3) Then
FoundMatch = 1
End If
Next
用下面的几行
On Error Resume Next
foundMatch = WorksheetFunction.Match(Saved_User_Input(i, 3), WorksheetFunction.Index(AccountList, 0, 3), 0)
foundMatch = Abs(CBool(Err.Number = 0))
On Error GoTo 0
显示的代码是一个子例程,它比较两个数组中的数据,以查找出现在一个列表中但未出现在另一个列表中的帐号。如果它发现缺少一个,它会将帐户信息添加到 Sheet。在平常的一天,它会从 2,500 - 4,000 条记录中找到 10-20 个丢失的帐户,具体取决于我们一个月的时间。处理 i 循环需要 15-20 分钟。与 Sheet 的互动很少。我不知道如何改进代码以使其 运行 更快。赞赏建议。
For i = 1 To TempCount
If i Mod 5 = 0 Then DoEvents
FoundMatch = 0
percent = i / TempCount * 100
Application.StatusBar = "Checking for missing accounts. Processing row " & i & " of " & TempCount & " - " & percent & "%"
For y = 1 To OppListRow
If Saved_User_Input(i, 3) = AccountList(y, 3) Then
FoundMatch = 1
End If
Next
If FoundMatch = 0 Then
n = n + 1
Opportunities.Cells(n, 2) = Saved_User_Input(i, 1)
Opportunities.Cells(n, 3) = Saved_User_Input(i, 2)
Opportunities.Cells(n, 4) = Saved_User_Input(i, 3)
Opportunities.Cells(n, 5) = Saved_User_Input(i, 4)
Opportunities.Cells(n, 6) = Saved_User_Input(i, 5)
Opportunities.Cells(n, 7) = Saved_User_Input(i, 6)
Opportunities.Cells(n, 8) = Saved_User_Input(i, 7)
Opportunities.Cells(n, 9) = Saved_User_Input(i, 8)
Opportunities.Cells(n, 10) = Saved_User_Input(i, 9)
Opportunities.Cells(n, 11) = Saved_User_Input(i, 10)
Opportunities.Cells(n, 12) = Saved_User_Input(i, 11)
Opportunities.Cells(n, 13) = Saved_User_Input(i, 12)
Opportunities.Cells(n, 14) = Saved_User_Input(i, 13)
Opportunities.Cells(n, 15) = Saved_User_Input(i, 14)
Opportunities.Cells(n, 16) = Saved_User_Input(i, 15)
End If
Next
将所有你的Opportunities.Cells行替换为这个单个一个
Opportunities.Cells(n, 2).Resize(1, 15).Value2 = WorksheetFunction.Index(Saved_User_Input, i, 0)
编辑(消除一个循环)
替换下面的循环
For y = 1 To OppListRow
If Saved_User_Input(i, 3) = AccountList(y, 3) Then
FoundMatch = 1
End If
Next
用下面的几行
On Error Resume Next
foundMatch = WorksheetFunction.Match(Saved_User_Input(i, 3), WorksheetFunction.Index(AccountList, 0, 3), 0)
foundMatch = Abs(CBool(Err.Number = 0))
On Error GoTo 0