views:

116

answers:

1

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.

I cannot combine the 2 sheets and use filters because I'm doing dynamic SQL to query different data.

Each sheet has a unique key column

I'm ok with VBA suggestions and Excel formulas. Just as long as I don't combine sheets.

Thank so much guys!

Sorry, apparently I made a mistake. There is an infinite loop here somewhere. This is Ben's answer btw. I just reposted a compilable version.

    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


    keyColA = "A"
    keyColB = "A"

    intRowCounterA = 1
    intRowCounterB = 1

    Set wsA = Worksheets("Sheet2")
    Set wsB = Worksheets("Sheet1")

    Do While Not IsEmpty(wsA.Range(keyColA & intRowCounterA).Value)


        Set rngA = wsA.Range(keyColA & intRowCounterA)

         intRowCounterB = 1
        Do While Not IsEmpty(wsB.Range(keyColB & intRowCounterB).Value)

            Set rngB = wsB.Range(keyColB & intRowCounterB)

            If rngA.Value = rngB.Value Then

                 Rows(intRowCounterB).EntireRow.Delete
                 intRowCounterB = intRowCounterB - 1


            End If
              intRowCounterB = intRowCounterB + 1
        Loop
        intRowCounterA = intRowCounterA + 1
    Loop
End Sub
A: 
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.

Ben McCormack
Hi Ben, I tried your suggestion and it did not work.I changed the delete row line to ---- wsB.Range(Rows(intRowCounterB)).EntireRow.Delete but that didn't work either so any ideas as to what might be the problem? Thanks!
Ehsan
@Ehsan Have you stepped through the code to see if it's working correctly? I wrote all of that code last night without Excel handy, so while it seemed correct at the time, I didn't have a chance to fully debug it. Note also that you may need to change the sheet names of `"Sheet A"` and `"Sheet B"` in your code.
Ben McCormack
Ben, thanks for the reply. I did in fact change the code up as far as naming goes...no brainer, but I haven't gone through the code to check for syntax because I'm very new to VBA so I took your word for it. I'll take this as a learning opportunity and go learn the syntax and get back to you. Thanks.
Ehsan
Ben McCormack
Hey Ben, do you know any way we can make this faster? It's terribly slow with a couple 100 records.Thanks for your help!
Ehsan
@Ehsan I'm not exactly sure without going through it line by line. I made a quick change to my code so that it saves the value from Sheet A, which should speed up the process somewhat.
Ben McCormack