Macro not running as originally planned

ghrek

Active Member
Joined
Jul 29, 2005
Messages
262
Hi
brings
I had this macro that points out duplicate and missing items. It brings up the missing on sheet 1 but for some reason does not delete 1 duplicated entry on sheet 2 and then mark other as DUPLICATE in column E.

Any Ideas?

VBA Code:
Sub DeleteIdenticalRecordsFromTwoSheets()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lr1 As Long, lr2 As Long, i As Long
Dim x, y, dict1 As Object, dict2 As Object
Dim delRng1 As Range, delRng2 As Range
Dim Txt As String
Application.ScreenUpdating = False

Set ws1 = Worksheets("sheet1")
Set ws2 = Worksheets("sheet2")

lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row


lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
x = ws1.Range("A1:D" & lr1).Value
y = ws2.Range("A1:D" & lr2).Value

Set dict1 = CreateObject("Scripting.Dictionary")
Set dict2 = CreateObject("Scripting.Dictionary")

For i = 1 To UBound(x, 1)
   Txt = x(i, 1) & x(i, 2) & x(i, 3) & x(i, 4)
   If Not dict1.exists(Txt) Then
      dict1.Add Txt, Array(i, 1)
   Else
      dict1(Txt) = Array(dict1(Txt)(0), dict1(Txt)(1) + 1)
   End If
Next i

For i = 1 To UBound(y, 1)
    Txt = y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)
    If Not dict2.exists(Txt) Then dict2.Add Txt, i
Next i
For i = 1 To UBound(x, 1)
   Txt = x(i, 1) & x(i, 2) & x(i, 3) & x(i, 4)
    If dict2.exists(Txt) Then
        If delRng1 Is Nothing Then
            Set delRng1 = ws1.Rows(i)
        Else
            Set delRng1 = Union(delRng1, ws1.Rows(i))
        End If
    Else
        ws1.Range("E" & i) = "Missing"
    End If
Next i


For i = 1 To UBound(y, 1)
   Txt = y(i, 1) & y(i, 2) & y(i, 3) & y(i, 4)
   If dict1.exists(Txt) Then
      If dict1(Txt)(1) = 0 Then
         ws2.Cells(i, 5) = "Duplicate"
      Else
         dict1(Txt) = Array(dict1(Txt)(0), dict1(Txt)(1) - 1)
         If delRng2 Is Nothing Then
            Set delRng2 = ws2.Rows(i)
         Else
            Set delRng2 = Union(delRng2, ws2.Rows(i))
         End If
      End If
   End If
Next i
If Not delRng1 Is Nothing Then delRng1.Delete
If Not delRng2 Is Nothing Then delRng2.Delete
End Sub
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Watch MrExcel Video

Forum statistics

Threads
1,128,165
Messages
5,629,068
Members
416,363
Latest member
zaveedd

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
Top