VBA - 如何使带有 For 循环和 IF 语句的代码更高效?
VBA - How to make code with For Loops and IF Statements more efficient?
我编写了一个代码来匹配数据(MaterialPN 与 MaterialPS 以及 WeekPN 与 WeekPS)并在两张纸之间复制适当的值(包装需求 - PN 和包装分段 - PS).
我已经关闭了屏幕更新、计算和事件。这使得 运行 时间从 5 分钟减少到 1 分钟,这仍然很慢(我的数据只有 ~3000 行)。当 WeekPN 不等于 WeekPS 时,我还尝试使用 GoTo Flag1 强制退出 If 语句,但这并没有使我的代码 运行 更快。
关于如何使此代码更高效的任何提示?
在此先感谢您的帮助!
Sub PackagingNeeds2PackagingStaging()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
With Sheets("Packaging Needs")
i = .Cells(.Rows.Count, 5).End(xlUp).Row
End With
With Sheets("Packaging Staging")
l = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
j = 25
For k = 9 To i
For x = 5 To l
For Z = 14 To j
MaterialPN = Sheets("Packaging Needs").Cells(k, 5).Value
MaterialPS = Sheets("Packaging Staging").Cells(x, 1).Value
WeekPN = Sheets("Packaging Needs").Cells(4, Z).Value
WeekPS = Sheets("Packaging Staging").Cells(x, 12).Value
If WeekPN <> WeekPS Then GoTo Flag1
If WeekPN = WeekPS Then
If MaterialPN = MaterialPS Then
Sheets("Packaging Staging").Cells(x, 19).Value = Sheets("Packaging Needs").Cells(k, Z).Value
End If
End If
Flag1:
Next
Next
k = k + 5
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
一些建议 - 使用 Variant 数组可能会更快,如果 Match 在这里合适(很难说不知道你期望进行多少次匹配 - 如果只有一个那么你也可以 Exit For
找到匹配项后跳出循环)
Sub PackagingNeeds2PackagingStaging()
Const J As Long = 25 'use Const for fixed values
Dim i As Long, x As Long, l As Long, k As Long, z As Long
Dim shtPN As Worksheet, shtPS As Worksheet
Dim MaterialPN, MaterialPS, WeekPS
'use worksheet variables
Set shtPN = ThisWorkbook.Sheets("Packaging Needs")
Set shtPS = ThisWorkbook.Sheets("Packaging Staging")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
i = shtPN.Cells(shtPN.Rows.Count, 5).End(xlUp).Row
l = shtPS.Cells(shtPS.Rows.Count, 1).End(xlUp).Row
For k = 9 To i Step 5 '<< use Step instead of your line below
MaterialPN = shtPN.Cells(k, 5).Value '<< moved this up
For x = 5 To l
MaterialPS = shtPS.Cells(x, 1).Value '<< moved this up
WeekPS = shtPS.Cells(x, 12).Value '<< ...and this
For z = 14 To J
If shtPN.Cells(4, z).Value = WeekPS Then
If MaterialPN = MaterialPS Then
shtPS.Cells(x, 19).Value = shtPN.Cells(k, z).Value
End If
End If
Next z
Next x
'k = k + 5 '<<< don't change the counter inside a For loop!
Next k
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
我编写了一个代码来匹配数据(MaterialPN 与 MaterialPS 以及 WeekPN 与 WeekPS)并在两张纸之间复制适当的值(包装需求 - PN 和包装分段 - PS).
我已经关闭了屏幕更新、计算和事件。这使得 运行 时间从 5 分钟减少到 1 分钟,这仍然很慢(我的数据只有 ~3000 行)。当 WeekPN 不等于 WeekPS 时,我还尝试使用 GoTo Flag1 强制退出 If 语句,但这并没有使我的代码 运行 更快。
关于如何使此代码更高效的任何提示?
在此先感谢您的帮助!
Sub PackagingNeeds2PackagingStaging()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
With Sheets("Packaging Needs")
i = .Cells(.Rows.Count, 5).End(xlUp).Row
End With
With Sheets("Packaging Staging")
l = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
j = 25
For k = 9 To i
For x = 5 To l
For Z = 14 To j
MaterialPN = Sheets("Packaging Needs").Cells(k, 5).Value
MaterialPS = Sheets("Packaging Staging").Cells(x, 1).Value
WeekPN = Sheets("Packaging Needs").Cells(4, Z).Value
WeekPS = Sheets("Packaging Staging").Cells(x, 12).Value
If WeekPN <> WeekPS Then GoTo Flag1
If WeekPN = WeekPS Then
If MaterialPN = MaterialPS Then
Sheets("Packaging Staging").Cells(x, 19).Value = Sheets("Packaging Needs").Cells(k, Z).Value
End If
End If
Flag1:
Next
Next
k = k + 5
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
一些建议 - 使用 Variant 数组可能会更快,如果 Match 在这里合适(很难说不知道你期望进行多少次匹配 - 如果只有一个那么你也可以 Exit For
找到匹配项后跳出循环)
Sub PackagingNeeds2PackagingStaging()
Const J As Long = 25 'use Const for fixed values
Dim i As Long, x As Long, l As Long, k As Long, z As Long
Dim shtPN As Worksheet, shtPS As Worksheet
Dim MaterialPN, MaterialPS, WeekPS
'use worksheet variables
Set shtPN = ThisWorkbook.Sheets("Packaging Needs")
Set shtPS = ThisWorkbook.Sheets("Packaging Staging")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
i = shtPN.Cells(shtPN.Rows.Count, 5).End(xlUp).Row
l = shtPS.Cells(shtPS.Rows.Count, 1).End(xlUp).Row
For k = 9 To i Step 5 '<< use Step instead of your line below
MaterialPN = shtPN.Cells(k, 5).Value '<< moved this up
For x = 5 To l
MaterialPS = shtPS.Cells(x, 1).Value '<< moved this up
WeekPS = shtPS.Cells(x, 12).Value '<< ...and this
For z = 14 To J
If shtPN.Cells(4, z).Value = WeekPS Then
If MaterialPN = MaterialPS Then
shtPS.Cells(x, 19).Value = shtPN.Cells(k, z).Value
End If
End If
Next z
Next x
'k = k + 5 '<<< don't change the counter inside a For loop!
Next k
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub