Excel / VBA Remove duplicate rows by cross referencing 2 different sheets then deleting 1 row

前端 未结 1 2047
孤独总比滥情好
孤独总比滥情好 2021-01-27 04:11

I have 2 separate sheets, lets call them sheet A, sheet B. I have data in sheet B which is also in sheet A. I want to find those rows that are equal and remove them from sheet B

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-27 04:39

    Sub CleanDupes()
        Dim wsA As Worksheet
        Dim wsB As Worksheet
        Dim keyColA As String
        Dim keyColB As String
        Dim rngA As Range
        Dim rngB As Range
        Dim intRowCounterA As Integer
        Dim intRowCounterB As Integer
        Dim strValueA As String
    
    
        keyColA = "A"
        keyColB = "B"
    
        intRowCounterA = 1
        intRowCounterB = 1
    
        Set wsA = Worksheets("Sheet A")
        Set wsB = Worksheets("Sheet B")
    
        Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value)
            intRowCounterB = 1
            Set rngA = wsA.Range(keyColA & intRowCounterA)
            strValueA = rngA.Value
            Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value
                Set rngB = wsB.Range(keyColB & intRowCounterB)
                If strValueA = rngB.Value Then
                     'Code to delete row goes here, but I'm not sure exactly'
                     'what it is.'
                     wsB.Rows(intRowCounterB).Delete
                     intRowCounterB = intRowCounterB - 1
                End If
                intRowCounterB = intRowCounterB + 1
            Loop
            intRowCounterA = intRowCounterA + 1
        Loop
    End Sub
    

    That should get you started.

    0 讨论(0)
提交回复
热议问题