VBA - Identify duplicate values in column

JetSetDrive

New Member
Joined
Jul 26, 2019
Messages
14
I need assistance with a vba code that will identify duplicate values in a column and copy those cells into a new tab labeled duplicate.

Thank you in advance
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this

The macro creates the "duplicate" sheet

Code:
Sub Macro2()
  Dim rn As Range, sh As Worksheet
  Set sh = ActiveSheet
  Set rn = sh.Range("A1", sh.Range("A" & Rows.Count).End(xlUp))
  rn.FormatConditions.AddUniqueValues
  rn.FormatConditions(rn.FormatConditions.Count).SetFirstPriority
  rn.FormatConditions(1).DupeUnique = xlDuplicate
  rn.FormatConditions(1).Interior.Color = 255
  rn.FormatConditions(1).StopIfTrue = False
  sh.Range("A1").AutoFilter Field:=1, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
  Sheets.Add(, Sheets(Sheets.Count)).Name = "Duplicate"
  sh.AutoFilter.Range.EntireRow.Copy Range("A1")
  Cells.FormatConditions.Delete
  rn.FormatConditions.Delete
  sh.ShowAllData
End Sub
 
Upvote 0
Here's another macro for you to consider. Assumes you have a header in A1 and values to be checked for duplicates and creates the sheet duplicate.

Code:
Sub ExtractDups()
Dim Din As Object, Vin As Variant, Dout As Object, i As Long, ct As Long
Set Din = CreateObject("scripting.dictionary")
Set Dout = CreateObject("scripting.dictionary")
Vin = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
For i = 1 To UBound(Vin, 1)
    If Not Din.exists(Vin(i, 1)) Then
        Din.Add Vin(i, 1), ""
    ElseIf Not Dout.exists(Vin(i, 1)) Then
        ct = ct + 1
        Dout.Add Vin(i, 1), ""
    End If
Next i
If ct > 0 Then
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With
    On Error Resume Next
    Sheets("duplicate").Delete
    On Error GoTo 0
    Sheets.Add after:=Sheets(Sheets.Count)
    With ActiveSheet
        .Name = "duplicate"
        .Range("A2:A" & ct + 1).Value = Application.Transpose(Dout.keys)
        .Range("A1").Value = "Dup Values"
    End With
    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
Else
    MsgBox "No duplicates in target range"
End If
End Sub
 
Last edited:
Upvote 0
i appreciate the help with this.

One last thing, the data I am looking for is in column g and I want the other columns and headers brought over as well.

Thank you
 
Upvote 0
i appreciate the help with this.

One last thing, the data I am looking for is in column g and I want the other columns and headers brought over as well.

Thank you


Try this

Code:
Sub Macro2()
  Dim rn As Range, sh As Worksheet
  Set sh = ActiveSheet
  If sh.AutoFilterMode Then sh.AutoFilterMode = False
  Set rn = sh.Range("G1", sh.Range("G" & Rows.Count).End(xlUp))
  rn.FormatConditions.AddUniqueValues
  rn.FormatConditions(rn.FormatConditions.Count).SetFirstPriority
  rn.FormatConditions(1).DupeUnique = xlDuplicate
  rn.FormatConditions(1).Interior.Color = 255
  rn.FormatConditions(1).StopIfTrue = False
  sh.Range("A1:G1").AutoFilter Field:=7, Criteria1:=RGB(255, 0, 0), Operator:=xlFilterCellColor
  Sheets.Add(, Sheets(Sheets.Count)).Name = "Duplicate"
  sh.AutoFilter.Range.EntireRow.Copy Range("A1")
  Cells.FormatConditions.Delete
  rn.FormatConditions.Delete
  sh.ShowAllData
End Sub

Note: It would help if from the beginning you provide in greater detail what you need, that way we could give you a more complete solution.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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