Find a duplicate value in the column using VBA code

krishna008

New Member
Joined
Jan 7, 2010
Messages
12
I have an excel file with "Person" tab. I would like to check if any duplicate values are there in the column I, if duplicate values are present in Column I, we need to list them in a a new work book name as "anomalies report" with column headers as "Duplicate Value" and "Duplicate Columns" and list out all the duplicates in the anomalies sheet of "Anomalies Report" save it on desktop. If no anomalies don't create the new workbook.

Can I get the VBA code for this please?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
with column headers as "Duplicate Value" and "Duplicate Columns"
I'm not sure what data you want in each column.
It would help if you gave an example of what you have and the expected result.

Try the following macro, in the first column are the duplicate values:

VBA Code:
Sub finddups()
  Dim sh As Worksheet
  Dim wb2 As Workbook
  Dim dic As Object
  Dim a As Variant
  Dim i As Long, j As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh = ThisWorkbook.Sheets("Person")
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = sh.Range("I1", sh.Range("I" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then
      j = j + 1
      b(j, 1) = a(i, 1)
    End If
    dic(a(i, 1)) = i
  Next
  
  If j = 0 Then
    MsgBox "No anomalies"
  Else
    Set wb2 = Workbooks.Add
    Range("A1:B1").Value = Array("Duplicate Value", "Duplicate Columns")
    Range("A2").Resize(j).Value = b
    wb2.SaveAs Environ("USERPROFILE") & "\Desktop" & "\Anomalies Report", xlOpenXMLWorkbook
    wb2.Close
    MsgBox "Anomaly report created"
  End If
  
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

I hope to hear from you soon.
Respectfully
Dante Amor
_______________
 
Upvote 0
Thanks for the reply. I'm getting 400 error when executing
Set wb2 = Workbooks.Add
Range("A1:B1").Value = Array("Duplicate Value", "Duplicate Columns").
 
Upvote 0
Thanks for the reply. I'm getting 400 error when executing
Set wb2 = Workbooks.Add
Range("A1:B1").Value = Array("Duplicate Value", "Duplicate Columns").
On which line?

Try again:
VBA Code:
Sub finddups()
  Dim sh As Worksheet
  Dim wb2 As Workbook
  Dim dic As Object
  Dim a As Variant
  Dim i As Long, j As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set sh = ThisWorkbook.Sheets("Person")
  Set dic = CreateObject("Scripting.Dictionary")
  
  a = sh.Range("I1", sh.Range("I" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  
  For i = 1 To UBound(a)
    If dic.exists(a(i, 1)) Then
      j = j + 1
      b(j, 1) = a(i, 1)
    End If
    dic(a(i, 1)) = i
  Next
  
  If j = 0 Then
    MsgBox "No anomalies"
  Else
    Set wb2 = Workbooks.Add
    Range("A1").Value = "Duplicate Value"
    Range("A2").Resize(j).Value = b
    wb2.SaveAs Environ("USERPROFILE") & "\Desktop" & "\Anomalies Report", xlOpenXMLWorkbook
    wb2.Close
    MsgBox "Anomaly report created"
  End If
  
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,996
Members
452,542
Latest member
Bricklin

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