简体   繁体   English

使用Excel VBA从随机单元格中删除重复项

[英]Remove Duplicates from Random Cells using Excel VBA

I have an excel sheet where I've duplicate values in difference cells. 我有一个Excel工作表,其中我在不同的单元格中重复了值。 BUT the catch here is all those cells are not adjacent to one another. 但是这里要抓住的是所有这些单元都不相邻。 I'll randomly select those cells manually from the sheets & want to remove the duplicates. 我将从表格中手动随机选择这些单元格,并希望删除重复的单元格。

In below screenshot I've selected random cells with value "test". 在下面的屏幕截图中,我选择了值为“ test”的随机单元格。 I would like to remove the duplicates from selected cells. 我想从选定的单元格中删除重复项。

Apologies : Adding possible scenario. 道歉:添加可能的方案。 Need only first occurrence of any repetitive cells. 仅需要任何重复单元的第一次出现。 Remove remaining occurrences. 删除剩余的事件。 It means it should give A1=TEST & B6=WEST. 这意味着应该给出A1 = TEST&B6 = WEST。 all other cell values should be removed. 所有其他单元格值都应删除。

在此处输入图片说明

问题截图以供参考

Assuming that you have already made the random selection: 假设您已经进行了随机选择:

Sub dural()
    Dim v As Variant, r As Range
    v = ActiveCell.Text
    addy = ActiveCell.Address
    For Each r In Selection
        If Not addy = r.Address Then
            If r.Value = v Then
                r.ClearContents
            End If
        End If
    Next r
End Sub

Just for fun, here's a non-looping version. 只是为了好玩,这是一个非循环版本。 It does wipe out the ActiveCell's value and then reassign it, which worked in all situations in my limited testing: 它确实会清除ActiveCell的值,然后重新分配它,这在我有限的测试中适用于所有情况:

Sub RemoveAllSelectionCellsExceptActiveCell()
Dim ActiveCellValue As Variant

ActiveCellValue = ActiveCell.Formula
Selection.Clear
ActiveCell.Formula = ActiveCellValue
End Sub

EDIT: Response to your edited question 编辑:对您编辑的问题的答复

This relies on the fact that adding a duplicate to a collection generates an error . 这取决于将重复项添加到集合会产生错误的事实。 If that happens, the cell in question is added to a range of cells to delete. 如果发生这种情况,则将相关单元格添加到要删除的一系列单元格中。 Note that it will treat a cell with "=2" as different from a cell with "2": 请注意,它将把“ = 2”的单元格与“ 2”的单元格视为不同:

Sub RemoveAllSelectionCellsExceptActiveCell2()

Dim cell As Excel.Range
Dim collDupes As Collection
Dim DupeCells As Excel.Range

Set collDupes = New Collection
For Each cell In Selection.Cells
    On Error Resume Next
    collDupes.Add cell.Formula, cell.Formula
    If Err.Number <> 0 Then
        If DupeCells Is Nothing Then
            Set DupeCells = cell
        Else
            Set DupeCells = Union(DupeCells, cell)
        End If
    End If
    On Error GoTo 0
Next cell
DupeCells.Clear
End Sub

And another... 还有一个

If you want to clear the cells' contents and formatting and leave the cursor in the ActiveCell with no selected cells highlighting. 如果要清除单元格的内容和格式,并将光标留在ActiveCell ,而没有选中的单元格突出显示。

Note, when you make your selection, it will be the last cell visited that is the ActiveCell whose contents will remain, and remain selected. 请注意,当您进行选择时,它将是最后访问的单元格,即ActiveCell,其内容将保留并保持选中状态。

Option Explicit
Sub remSelDup()
Dim ac As Range, c As Range
Set ac = ActiveCell
    For Each c In Selection
        If c = ac And c.Address <> ac.Address Then
           c.Clear
        End If
    Next c
ac.Select
End Sub

There should be more than a few Find/FindNext examples on this site but here's another one. 这个站点上应该有不止几个Find/FindNext示例,但这是另一个示例。

Dim fnd As Range, fcl As Range, searchTerm As Variant

With ActiveSheet
    Set fcl = ActiveCell
    searchTerm = fcl.Value
    Set fnd = .Cells.Find(What:=searchTerm, After:=fcl, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    Do While fcl.Address <> fnd.Address
        fnd.ClearContents
        Set fnd = .Cells.FindNext(After:=fcl)
    Loop
End With

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

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