简体   繁体   中英

Delete the duplicated rows if it had 2 value in its Rth column row in excel 2007 vba

ActiveSheet.Range(cells("$2", "$A"), cells("$" & CStr(mowz), "$Q"))._
RemoveDuplicates                         
Columns:=Array(1, 2, 6, 7, 8, 9), Header:=xlYes 

This the Macro, I recorded and using it to delete duplicates in excel 2007 vba.But I got a new task to solve.That is I have to remove the duplicated rows,if and only if its "Rth" Column has value 2 in it, else it should not delete it even though it is a duplicate

Is there any way to put a condition into the duplicate rows macro?Please let me know. And any suugestions are accepted

In my sheet I have 16 columns and The above macro Deletes the duplicates if Columns 1,2,6,7,8,9 has same values in it but the thing is, It must delete it if it has all the 6 columns duplicated and also a "2" value in its Rth column and it should not delete if Rth column has someother value even though all the six columns are same.

I don't know about your other code so I can't integrate this, but here is a sub() that will go through your R column and if it finds a "2" inside, it will delete the entire row. You can always add this to your other code by adding "Call Delete2s" to it ("call" is optional, but I tend to include it).

Sub Delete2s()
Range(Cells(1, "R"), Cells(Rows.Count, "R").End(xlUp)).Select

Dim lastRow As Long
lastRow = ActiveSheet.UsedRange.Rows.Count

For i = lastRow To 1 Step -1
    If Selection.Rows(i).Value = 2 Then
        Selection.Rows(i).EntireRow.Delete
    End If
Next 
End Sub

How it works : It finds the last cell used in column R (you can adjust this) and then loops through it backwards (you need to do this when you delete cells, otherwise you'll mess up the loop). If the value is 2, it deletes the entire row!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM