回答
這將清除重複數據如果搜索欄始終在列d和兩個其他的都是在B型和F
注意:這會直接刪除中間列中的數據,實際上並沒有填補剩下的空白。
Sub deleteThreeColDupes()
Dim sourceRange As range
Dim colOne As range
Dim colTwo As range
Dim myCell As range
Dim checkCell As range
'Set the search ranges
Set colOne = range("B2", Cells(Rows.count, 2).End(xlUp))
Set colTwo = range("F2", Cells(Rows.count, 6).End(xlUp))
Set sourceRange = range("D2", Cells(Rows.count, 4).End(xlUp))
'Compare with the first column. If there is a match, clear the value and exit the loop.
'if no match in first column, compare with the second column.
For Each myCell In sourceRange
For Each checkCell In colOne
If myCell.Value = checkCell.Value Then
myCell.Value = ""
Exit For
End If
Next checkCell
If myCell.Value <> "" Then
For Each checkCell In colTwo
If myCell.Value = checkCell.Value Then
myCell.Value = ""
Exit For
End If
Next checkCell
End If
Next myCell
'Clear sets
Set colOne = Nothing
Set colTwo = Nothing
Set sourceRange = Nothing
End Sub
爲什麼不使用'Range.Find'?它會比在列上迭代更快...... –
@LoganReed要變得生硬,只是因爲我不熟悉這種方法。如果它有效,那太棒了!我以前沒有用過它。 – PartyHatPanda
[你在這裏](https://msdn.microsoft.com/en-us/library/office/ff839746.aspx)。這是非常值得的時間! –
使用集合的一個更有效的版本。它僅對列B和F進行一次迭代,並且可以在迭代結果集合中立即查找值。
Sub deleteDups()
' setup column ranges
Dim rngB As Range
Dim rngD As Range
Dim rngF As Range
With ActiveSheet
Set rngB = .Range(.[b2], .[b2].End(xlDown))
Set rngD = .Range(.[d2], .[d2].End(xlDown))
Set rngF = .Range(.[f2], .[f2].End(xlDown))
End With
' store columns B and F in collections with value = key
Dim colB As New Collection
Dim colF As New Collection
Dim c As Range
For Each c In rngB: colB.Add c, c: Next
For Each c In rngF: colF.Add c, c: Next
' quickly check if the value in any of the columns
For Each c In rngD
If contains(colB, CStr(c)) Or contains(colF, CStr(c)) Then
Debug.Print "Duplicate """ & c & """ at address " & c.Address
' c.Clear ' clears the duplicate cell
End If
Next
End Sub
Function contains(col As Collection, key As String) As Boolean
On Error Resume Next
col.Item key
contains = (Err.Number = 0)
On Error GoTo 0
End Function
輸出:
Duplicate "cry" at address $D$4
Duplicate "car" at address $D$5
Duplicate "cat" at address $D$6
感謝您的幫助 – johndoe253
- 1. 比較列表和刪除重複
- 2. 比較屬性和刪除多個列表中的重複項
- 3. 馬克並刪除重複比較
- 4. 比較2 ArrayLists並刪除重複項
- 5. 比較2D陣列的第1列並刪除重複的Python
- 6. ActionScript - 比較和刪除複雜數組的重複項?
- 7. 比較器的比較()函數從地圖刪除重複
- 8. 比較陣列和刪除陣列
- 9. VBA刪除列表框重複
- 10. 比較列 - VBA
- 11. 刪除重複記錄比較表中的5列
- 12. 刪除重複的對象後,比較表2列出
- 13. 比較兩個陣列並刪除重複項
- 14. 比較在Excel中兩列並刪除重複
- 15. 比較兩個陣列刪除重複項
- 16. Excel的VBA比較,條件,刪除
- 17. 刪除重複項excel vba
- 18. VBA Excel刪除重複行
- 19. 比較2列和刪除不匹配
- 20. 比較2陣列和刪除元素
- 21. 刪除重複和陣列
- 22. 比較文件夾和刪除重複項
- 23. 比較和從MySQL刪除重複分貝
- 24. 比較文件,並刪除重複的星火和Scala
- 25. 比較和刪除清單
- 26. 比較兩個數組和原數組的Java刪除重複(沒有列出)
- 27. sql在兩列上選擇3列和重複數據刪除
- 28. Excel VBA比較列數據複製行
- 29. VBA列值比較
- 30. 查找重複和比較
請出示至少有一些試圖在自己的解決問題的努力.. –
感謝@UlliSchmid – johndoe253