2017-07-22 47 views
0

需要幫助刪除行,我有下面這段代碼:包含重複值

Dim LRAS, matchFoundIndex, iCntr As Long 
LRAS = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row 

For iCntr = 3 To LRAS 
    If Cells(iCntr, 1) <> "" Then 
    matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & LRAS), 0) 
    If iCntr <> matchFoundIndex Then 
     Cells(iCntr, 14) = "Duplicate" 
     'at this point duplicate 
    End If 
End If 

Next iCntr 
'----------- 
Application.Calculation = xlCalculationManual 
Application.EnableEvents = False 
Application.ScreenUpdating = False 
Dim deleteRow As Long 
Dim wrksht As Worksheet 
Set wrksht = ActiveSheet 

For deleteRow = wrksht.Range("N" & Rows.Count).End(xlUp).Row To 3 Step -1 
If wrksht.Range("N" & deleteRow).Value = "Duplicate" Then 
    Rows(deleteRow).EntireRow.Delete 
End If 

Next deleteRow 
Application.Calculation = xlCalculationAutomatic 
Application.EnableEvents = True 
Application.ScreenUpdating = True 

此代碼basicly搜索重複值,然後說,值是重複的。下一步是刪除第二個重複項目。 我想將此構建成一個事件而不是兩個分離的事件。有沒有辦法做到這一點?還是有另一種方法來刪除基於第一列中的值的重複行?

回答

2

我認爲,所有你需要的是一個說法:

Cells.RemoveDuplicates 1 

其中1表示第一列,A,在複印件的,應當檢查並刪除。

如果按鍵分佈在多列,說列1和3,您可以根據這些列這樣的刪除重複:

Cells.RemoveDuplicates Array(1, 3) 
+0

謝謝,我要去嘗試了這一點.. – Tarik