【发布时间】:2016-12-10 15:41:58
【问题描述】:
我想优化以下代码,因为它很慢。 我正在使用此答案中的代码: https://stackoverflow.com/a/27108055/1042624
但是,循环通过 +10k 行时速度非常慢。是否可以在下面优化我的代码?我试着修改了一下,但似乎没有用。
Sub DeleteCopy2()
Dim LastRow As Long
Dim CurRow As Long
Dim DestLast As Long
Dim strSheetName As String
Dim arrVal() As Long
Application.ScreenUpdating = False
Application.Calculation = xlManual
strSheetName = "Week " & ISOWeekNum(Format(Date)) - 1
LastRow = Sheets("MatchData").Range("A" & Rows.Count).End(xlUp).Row
DestLast = Sheets(strSheetName).Range("A" & Rows.Count).End(xlUp).Row
ReDim arrVal(2 To LastRow) ' Headers in row 1
For CurRow = LBound(arrVal) To UBound(arrVal)
If Not Sheets(strSheetName).Range("A2:A" & DestLast).Find(Sheets("MatchData").Range("A" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
Sheets("MatchData").Range("A" & CurRow).Value = ""
Else
End If
Next CurRow
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
【问题讨论】:
-
我看不到您将工作表数据复制到
arrVal数组的位置。ReDim当然会根据您的数据定义它的大小,但您没有使用它。 -
您想检查
Sheets("MatchData").Range("A" & CurRow).Value是否存在于Sheets(strSheetName).Range("A2:A" & DestLast)中,然后它会清除它吗? -
@PeterT 你说得对,我在“jungle”数组中有点迷失了。
-
@SiddharthRout 完全正确。实际上,我想删除它们,但出于测试目的,我暂时将其留空,直到它起作用为止。
-
难怪它很慢......你需要使用两个数组......让我在发布解决方案之前测试一下
标签: arrays excel for-loop optimization vba