简体   繁体   English

在存在重复项的Excel中删除行

[英]Remove Rows in excel where duplicates are present

Suppose I have an excel sheet with 3 columns as Name, Age and Number. 假设我有一个Excel工作表,其中有3列,分别是名称,年龄和数字。 Names are A,B and C. Ages are 10,11 and 12. And Number are 5,7 and 5. In Number column, there is a duplicate value 5 in the third row which is already present in the first row. 名称是A,B和C。年龄是10,11和12。数字是5,7和5。在“数字”列中,第三行中有重复的值5,该值已经出现在第一行中。 I need a method to remove the row where a duplicate value is present. 我需要一种方法来删除存在重复值的行。 Just keep the first row in case of duplicate values. 如果值重复,则保留第一行。 That means a way to remove C's entry keeping A's entry. 这意味着删除保留A条目的C条目的方法。

This is just a small scenario I need to do this with way large data. 这只是一个小场景,我需要对大量数据进行处理。

There is a standard ribbon button in Excel in the "Data" tab called "Remove duplicates". Excel中的“数据”选项卡中有一个标准的功能区按钮,称为“删除重复项”。 This should do exactly what you want: https://www.excel-easy.com/examples/remove-duplicates.html 这应该完全符合您的要求: https : //www.excel-easy.com/examples/remove-duplicates.html

Jaskunwar , looks like you did not give it a try else you could have found n number of solution as the problem looks very simple. Jaskunwar,您似乎没有尝试过,否则您可能会找到n个解决方案,因为问题看起来非常简单。 If your question is something else please describe. 如果您还有其他问题,请描述。

As Alex Suggested Remove Duplicate will solve your problem. 正如亚历克斯建议删除重复将解决您的问题。 You can use Conditional formatting, Pivot, If formula to achieve this. 您可以使用条件格式,数据透视,If公式来实现此目的。

What about this? 那这个呢? (select the range you need) (选择您需要的范围)

    Sub SpotUniques()

    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select range", xTitleId, WorkRng.address, Type:=8)
    WorkRng.Select

    With CreateObject("Scripting.Dictionary")

    For Each cell In WorkRng 
    .Item(cell.Value) = .Item(cell.Value) + 1
    If .Item(cell.Value) = 1 Then
    With cell.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = RGB(255, 102, 0)
    End With
    End If
    Next cell
    End With
    End Sub

You could try: 您可以尝试:

Option Explicit

Sub test()

    Dim Lastrow As Long, Times As Long, i As Long
    Dim rng As Range
    Dim str As String

    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last row of column A
        Lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row

        'Start Loop from the lastrow to row 1 upside down
        For i = Lastrow To 2 Step -1
            'Give value to str
            str = .Range("C" & i).Value
            'Set the range you want to search
            Set rng = .Range("C2:C" & Lastrow)
            'Count how many times str appears in rng
            Times = Application.WorksheetFunction.CountIf(rng, str)
            'If it is appears more than one time and A & i value equal to C
            If Times > 1 And .Range("A" & i).Value = "C" Then
                .Rows(i).EntireRow.Delete
            End If

        Next i

    End With

End Sub

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

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