Compare records in 2 columns, and delete repeated ones

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
In 2 contiguous columns, A and B, where column A will always have 50 records and in B, 30, I need a code that compares both columns, and if you find records from column B in column A, delete them from column A, and those left in column A, copy them to another column, column C.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I thought for sure some VBA ringer in here would have a solution to this.
I'm far from a ringer...very far...but I'm not only learning and following what the ringers have done but I look for a solution.
Even if my solution isn't ideal or ugly...I think I have a solution.

Did I understand your problem?

Code:
Sub FindDups()

Dim Aary As Variant, Bary As Variant, Cary(1 To 51) As String
Dim i As Long
Aary = Application.Transpose(Sheets("Sheet3").Range("A1:A51"))
Bary = Application.Transpose(Sheets("Sheet3").Range("B1:B31"))
' my test data is on Sheet3 in my test workbook and there are column headers in row 1
For i = 1 To 51
If IsError(Application.Match(Sheets("Sheet3").Cells(i, 1), Sheets("Sheet3").Range("B1:B31"), 0)) Then
  Cary(i) = Sheets("Sheet3").Cells(i, 1)
Else
Aary(i) = "REMOVE"
End If
Next i
For i = 1 To 51
Sheets("Sheet3").Cells(i, 1) = Aary(i) 
Sheets("Sheet3").Cells(i, 3) = Cary(i)
Next i
' now delete rows
For i = 51 To 1 Step -1
    If Range("A" & i) = "REMOVE" Or Range("C" & i) = "" Then
        Rows(i).Delete
    End If
Next i
For i = 1 To 31
Sheets("Sheet3").Range("B" & i) = Bary(i)
Next i
Sheets("Sheet3").Range("C1") = "New A"
End Sub
 
Upvote 0
Made an error in coping it. And, I think I made a mistake. SIGH

Code:
Sub FindDups()

Dim Aary As Variant, Bary As Variant, Cary(1 To 51) As String
Dim i As Long
Aary = Application.Transpose(Sheets("Sheet3").Range("A1:A51"))
OAary = Application.Transpose(Sheets("Sheet3").Range("A1:A51")) ' probably don't need the old A column
Bary = Application.Transpose(Sheets("Sheet3").Range("B1:B31"))
' my test data is on Sheet3 in my test workbook
For i = 1 To 51
If IsError(Application.Match(Sheets("Sheet3").Cells(i, 1), Sheets("Sheet3").Range("B1:B31"), 0)) Then
  Cary(i) = Sheets("Sheet3").Cells(i, 1)
Else
Aary(i) = "REMOVE"
End If
Next i
For i = 1 To 51
Sheets("Sheet3").Cells(i, 1) = Aary(i)
Sheets("Sheet3").Cells(i, 3) = Cary(i)
Next i
' now delete rows
For i = 51 To 1 Step -1
    If Range("A" & i) = "REMOVE" Or Range("C" & i) = "" Then
        Rows(i).Delete
    End If
Next i
For i = 1 To 31
Sheets("Sheet3").Range("B" & i) = Bary(i)
Next i
For i = 1 To 51
Sheets("Sheet3").Range("A" & i) = Aary(i)
Next i
Sheets("Sheet3").Range("C1") = "New A"
End Sub
 
Last edited:
Upvote 0
I think I fixed my stupidity.

Code:
Sub FindDups()

Dim Aary As Variant, Bary As Variant, Cary(1 To 51) As String
Dim i As Long
Aary = Application.Transpose(Sheets("Sheet3").Range("A1:A51"))
' OAary = Application.Transpose(Sheets("Sheet3").Range("A1:A51"))
Bary = Application.Transpose(Sheets("Sheet3").Range("B1:B31"))
' my test data is on Sheet3 in my test workbook
For i = 1 To 51
If IsError(Application.Match(Sheets("Sheet3").Cells(i, 1), Sheets("Sheet3").Range("B1:B31"), 0)) Then
  Cary(i) = Sheets("Sheet3").Cells(i, 1)
Else
Aary(i) = "REMOVE"
End If
Next i
For i = 1 To 51
 Sheets("Sheet3").Cells(i, 1) = Aary(i)
 Sheets("Sheet3").Cells(i, 3) = Cary(i)
Next i
' now delete rows
For i = 51 To 1 Step -1
    If Range("A" & i) = "REMOVE" Then
    'If Range("A" & i) = "REMOVE" Or Range("C" & i) = "" Then
        Rows(i).Delete
    End If
Next i
For i = 1 To 31
 Sheets("Sheet3").Range("B" & i) = Bary(i)
Next i
For i = 1 To 51
 If Aary(i) <> "REMOVE" Then
 Sheets("Sheet3").Range("C" & i) = Aary(i)
 Else
 End If
Next i
Sheets("Sheet3").Range("C1") = "New A"
End Sub
 
Upvote 0
Well
What about
VBA Code:
Sub test()
    Dim a, b As Variant, i
    a = Range("a1:a50")
    b = Range("b1:b30")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a): If Not .exists(a(i, 1)) Then .Add a(i, 1), 1: Next
        For i = 1 To UBound(b): If .exists(b(i, 1)) Then .Remove b(i, 1): Next
        Cells(1, 3).Resize(.Count) = Application.Transpose(.keys)
    End With
End Sub
 
Upvote 0
Solution
Well
What about
VBA Code:
Sub test()
    Dim a, b As Variant, i
    a = Range("a1:a50")
    b = Range("b1:b30")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a): If Not .exists(a(i, 1)) Then .Add a(i, 1), 1: Next
        For i = 1 To UBound(b): If .exists(b(i, 1)) Then .Remove b(i, 1): Next
        Cells(1, 3).Resize(.Count) = Application.Transpose(.keys)
    End With
End Sub
The following error occurs when executing the code:

Compilation error:
Next without For
 
Upvote 0
Then try
VBA Code:
Sub test()
    Dim a, b As Variant, i
    a = Range("a1:a50")
    b = Range("b1:b30")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1)) Then .Add a(i, 1), 1
        Next
        For i = 1 To UBound(b)
            If .exists(b(i, 1)) Then .Remove b(i, 1)
        Next
        Cells(1, 3).Resize(.Count) = Application.Transpose(.keys)
    End With
End Sub
Let me know
 
Upvote 0
Then try
VBA Code:
Sub test()
    Dim a, b As Variant, i
    a = Range("a1:a50")
    b = Range("b1:b30")
    With CreateObject("scripting.dictionary")
        For i = 1 To UBound(a)
            If Not .exists(a(i, 1)) Then .Add a(i, 1), 1
        Next
        For i = 1 To UBound(b)
            If .exists(b(i, 1)) Then .Remove b(i, 1)
        Next
        Cells(1, 3).Resize(.Count) = Application.Transpose(.keys)
    End With
End Sub
Let me know
NICE.
 
Upvote 0
De nada
Y gracias por los comentarios
Sea feliz y seguro
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top