简体   繁体   English

如果在 excel 2007 vba 的第 R 列行中有 2 个值,则删除重复的行

[英]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这是宏,我记录并使用它删除 excel 2007 vba 中的重复项。但我有一个新任务要解决。那就是我必须删除重复的行,当且仅当它的“Rth”列中的值为 2 , 否则即使它是重复的也不应该删除它

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.在我的工作表中,我有 16 列,如果列 1、2、6、7、8、9 具有相同的值,则上述宏删除重复项,但问题是,如果所有 6 列都重复,则必须删除它并且在其第 R 列中还有一个“2”值,如果第 R 列具有其他值,即使所有六列都相同,它也不应该删除。

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.我不知道您的其他代码,所以我无法集成它,但这里有一个 sub(),它将 go 通过您的 R 列,如果它在里面找到“2”,它将删除整行。 You can always add this to your other code by adding "Call Delete2s" to it ("call" is optional, but I tend to include it).您始终可以通过向其中添加“Call Delete2s”将其添加到您的其他代码中(“call”是可选的,但我倾向于包含它)。

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).它是如何工作的:它找到 R 列中使用的最后一个单元格(您可以调整它),然后向后循环(删除单元格时需要这样做,否则会弄乱循环)。 If the value is 2, it deletes the entire row!如果值为 2,则删除整行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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