如何检查特定列上的重复项?

时间:2019-05-06 08:29:32

标签: excel vba

我正在尝试检测输入工作簿的column(“ G”)上的重复项,并通过使用&“”&在column(“ E”)处使用其数据的最后一行向上合并以将其合并,此后它将删除wholeRow和此过程将继续进行,直到没有更多重复项为止。

enter image description here

enter image description here

我尝试过并且还查找许多代码,包括删除和重复,但仍然遇到问题。

Dim myCell As Range, myRow As Integer, myRange As Range, myCol As Integer, X As Integer

    'Count number column

Set wsInput = Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112")

     myCol = Range(Cells(3, 7), Cells(3, 7).End(xlDown)).Count

     'Loop each column to check duplicate values & highlight them.

     For X = 3 To myRow
     Set myRange = Range(Cells(2, X), Cells(myRow, X))

     For Each myCell In myRange
     If Workbooks("InputB.xls").Worksheets("HC_MODULAR_BOARD_20180112").CountIf(myRange, myCell.Value) > 1 Then
     myCell.Interior.ColorIndex = 3

    End If
     Next
     Next


'  allow values at Column"E" to merge upwards and delete all duplicate and its row (missing)

复制列顶部的数据后,我不知道如何删除。有人帮忙。

非常感谢, 阿德里安

1 个答案:

答案 0 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long, y As Long, Counter As Long
    Dim SearchValue As String, AddValue As String

    With ThisWorkbook.Worksheets("Sheet1") ' Always select your worksheet name

        LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        Counter = 0
        AddValue = ""
        SearchValue = ""

        For i = LastRow To 3 Step -1

            SearchValue = .Range("C" & i).Value

            If SearchValue <> "" Then

                If Application.WorksheetFunction.CountIf(.Range("C3:C" & LastRow), SearchValue) > 1 Then

                    For y = i To 3 Step -1

                        If .Range("C" & y).Value = SearchValue Then

                            If AddValue = "" Then
                                AddValue = .Range("E" & y).Value
                            Else
                                AddValue = AddValue & ", " & .Range("E" & y).Value
                                .Rows(y).EntireRow.Delete
                                Counter = Counter + 1
                            End If

                        End If

                    Next y

                    .Range("E" & i - Counter).Value = AddValue
                    AddValue = ""
                    SearchValue = ""
                    Counter = 0

                End If

            End If

        Next i

    End With

End Sub