2014-03-24 22 views
0

我有一个代码,它可以比较同一工作表中的两个列表,并从两个列表中的一个列表中删除整行,但它现在运行非常缓慢(随着数据的增长)和我正在努力加快这一进程。试图加快EXCEL VBA中的列表比较

我不是在做这个以任何很大程度上随后,我找一些援助,

谢谢!

代码:

Sub Clean_Up_Lists() 
'run comparisons... clean up lists' 

'turn of screen updating to speed up macro' 
Application.ScreenUpdating = False 

Dim iListCount As Long 
Dim x As Range 
Dim iCtr As Long 


'get count of records to search through(list that will be deleted)' 
iListCount = Sheets("Allocations").Cells(Rows.Count, "B").End(xlUp).Row 


For Each x In Sheets("Allocations").Range("N200:N400" & Sheets("Allocations").Cells(Rows.Count, "B").End(xlUp).Row) 



'loop through all records in the second list' 
For iCtr = iListCount To 1 Step -1 

If x.Value = Sheets("Allocations").Cells(iCtr, 2).Value Then 
Sheets("Allocations").Cells(iCtr, 2).EntireRow.ClearContents 
'if match exists --> clear contents from allocations list' 

End If 
Next iCtr 
Next 

Application.ScreenUpdating = True 

End Sub 
+0

您预计这部分'的东西。 “N200:N400” 和表格( “分配”)细胞(Rows.Count“ B“)。结束(xlUp).Row'应该做什么? –

+0

这段代码实现了大约12个月,我的回忆是它定义了比较删除值的列表。 (基本上遍历主列表) 现在重新阅读它,我不知道这样做是否有冗余? – Guterres

+0

如果最后使用的拖拽,比如说500,这一行'“N200:N400”&Sheets(“Allocations”)。Cells(Rows.Count,“B”).End(xlUp).Row'给你'“N200 :N400500" '。不确定它是你需要的 –

回答

1

试试这个:

Sub Clean_Up_Lists() 
    Application.ScreenUpdating = False 

    Dim i As Long 
    Dim rng As Range, c As Range 
    Dim rngToClear As Range 
    Dim arr 

    With Sheets("Allocations") 
     Set rng = .Range("N200:N400") 
     arr = .Range("B1:B" & .Cells(.Rows.Count, "B").End(xlUp).Row).Value 
     For i = 1 To UBound(arr, 1) 
      If Not IsError(Application.Match(arr(i, 1), rng, 0)) Then 
       If rngToClear Is Nothing Then 
        Set rngToClear = .Range("B" & i) 
       Else 
        Set rngToClear = Union(rngToClear, .Range("B" & i)) 
       End If 
      End If 
     Next i 
    End With 

    If Not rngToClear Is Nothing Then rngToClear.EntireRow.ClearContents 

    Application.ScreenUpdating = True 
End Sub 
+1

谢谢simoco。 在我运行的测试文件中,它在家中的mac上运行良好。非常感激。 将在工作PC上测试它,我必须在明天真正运行它。 – Guterres