我想使用VBA删除两列B和C中的重复项。当B1 = B2且C1 = C2时,则应删除B2和C2数据。但是当B1!= B2 AND C1 = C2时,不应删除B2和C2,因为B2具有与B1不同的值。现在我正在使用下面的代码,但它没有按照我的意愿做正确的事情..它只删除了C列中的重复数据。
Sub ()
Dim rCell As Range
With Worksheets("Sheet1")
For Each rCell In Range("B1:C20")
rCell.EntireColumn.RemoveDuplicates 1
Next rCell
End With
End Sub
任何人都知道如何更改代码以使其正常工作?
提前致谢!
答案 0 :(得分:0)
我让你知心。...
Sub RemoveDups_Copy(sSheet As String)
Dim vArray As Variant
Dim x As Long, y As Integer
Dim sTest As String
Worksheets(sSheet).Select
x = 1
Do While Cells(x + 1, 1) <> ""
x = x + 1
Loop
lastRow = x
If lastRow = 1 Then lastRow = 2
lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
ReDim vArray(1 To lastRow, 1 To lastColumn)
Set dRemove = CreateObject("Scripting.Dictionary")
Set dRemoveIndex = CreateObject("Scripting.Dictionary")
For x = 2 To lastRow
sTest = ""
For y = 1 To lastColumn
sTest = sTest & "|" & Cells(x, y).Text
Next y
If dRemove(sTest) = "Remove" Then dRemoveIndex(x) = "Remove"
dRemove(sTest) = "Remove"
Next x
i = 0
For x = 1 To lastRow
If dRemoveIndex(x) <> "Remove" Then
i = i + 1
For y = 1 To lastColumn
vArray(i, y) = Cells(x, y).Text
Next y
End If
Next x
Range(Cells(2, 1), Cells(lastRow, lastColumn)).ClearContents
Call RemoveDups_Paste(1, 1, vArray)
End Sub
Sub RemoveDups_Paste(x As Integer, y As Integer, Arr As Variant)
Set Rng = Range(Cells(x, y), Cells(UBound(Arr, 1) - LBound(Arr, 1) + x, UBound(Arr, 2) - LBound(Arr, 2) + y))
Rng.Resize(UBound(Arr, 1) - LBound(Arr, 1) + 1, UBound(Arr, 2) - LBound(Arr, 2) + 1) = Arr
End Sub
这会将所有原始(或非重复)数据记录到一个数组中,清除数据,然后将其粘贴到一个范围内,而不是单独粘贴。应该跑得快。