给定行的值VBA删除重复项

时间:2016-01-13 14:19:38

标签: excel vba

我有一个包含数千行的电子表格,在B列中有许多重复项,然后在G列中有该行的相应值。我需要从列B中删除重复项,但保留在具有最高值的行中(即最大列G)。有没有办法通过VBA实现自动化,因为它需要在很多场合完成?

由于

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

Sub test()

Dim i As Long, j As Long
Dim wb As Workbook, ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1") ' Change the name of your sheet

With ws

i = 1 ' Start at Row 1

 Do While Not IsEmpty(.Cells(i, 2))

j = 1 ' Start at Row 1
    Do While Not IsEmpty(.Cells(j, 2))

    If i <> j Then

        If .Cells(i, 2).Value = Cells(j, 2).Value Then

                If .Cells(i, 7).Value > Cells(j, 7) Then

                    Rows(j).EntireRow.Delete
                    j = j - 1
                Else
                    Rows(i).EntireRow.Delete
                    If i > 1 Then i = i - 1
                    j = 1
                End If
        End If

    End If

    j = j + 1
    Loop

i = i + 1
Loop

End With

End Sub