Formula / Macro to find Duplicate Row based on multiple Columns conditions

Arafat

Board Regular
Joined
May 3, 2011
Messages
61
Hi All,

What would be the best way to find the duplicates using values from multiple columns.

Example : Concatenate value of columns A & B & C & D & G and find if this combination is repeated across the sheet and return/highlight value of Column A. (The sheet is large like 50,000 rows).

Thanks for your valuable help and time as always :)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try this on a copy of your data, it's something I got from this forum but never really used
Code:
Option Explicit

Sub FindDupeRows()
Dim LstRw As Long, ThsRw As Long, rng As Range
Application.ScreenUpdating = False

'/// This will remove any Highlighting prior to finding Dupes
With Rows("2:65536")
.Interior.ColorIndex = xlNone
End With

LstRw = Cells(Rows.count, "A").End(xlUp).Row
'/// This inserts a temporary column
Columns(1).Insert
With Cells(2, "A").Resize(LstRw - 1)
'/// This Joins everything in A, B, C, & D together for comparison
  .FormulaR1C1 = "=RC[1]&RC[2]&RC[3]&RC[4]&RC[7]"
  .Value = .Value
End With
Set rng = [A2].Resize(LstRw - 1)
For ThsRw = LstRw To 2 Step -1
  If Application.WorksheetFunction.CountIf(rng, Cells(ThsRw, "A").Value) > 1 Then _
  '/// This highlights Dupes
    Rows(ThsRw).Interior.ColorIndex = 27
Next
'/// Deletes temporary column
Columns(1).Delete

Application.ScreenUpdating = True
End Sub
 
Upvote 0
You will need and End If on this statement to avoid an error message.

If Application.WorksheetFunction.CountIf(rng, Cells(ThsRw, "A").Value) > 1 Then _
'/// This highlights Dupes
Rows(ThsRw).Interior.ColorIndex = 27
End If 'Add this to avoid error.
 
Upvote 0
Thanks for your timely help... :)

This works Great but when i try to concatenate a column which contains more data in a cell, it throws RUNTIME error 1004 "Unable to get the Countif worksheetfunction property ".

Does countif fail if cell contains large data ? i tried removing .WorksheetFunction but then i get Type mismatch error.

Thanks Zillions :) :)
 
Upvote 0
This may over come the countif problem you're experiencing:
Code:
Sub FindDupRows()

Dim i As Long, j As Long
Dim rng As Range

Application.ScreenUpdating = False

Cells.Interior.ColorIndex = xlNone

i = Range("A" & Rows.Count).End(xlUp).Row
With Range("A2:A" & i)
    .FormulaR1C1 = "=RC[1]&RC[2]&RC[3]&RC[4]"
    .Value = .Value
End With

Columns(1).Insert
Set rng = Range("B2:B" & i)
For j = 2 To i
    Range("A" & j).Formula = "=COUNTIF(" & rng & ",B" & i & ")"
    .Value = .Value
    If Range("A" & j) > 1 Then Rows(j).Interior.ColorIndex = 27
Next j
Columns(1).Delete

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thanks for your code :), however it give me compile error
in this piece of code . It stops near .Value and throws compile error "Invalid or Unqualified reference"

Columns(1).Insert
Set rng = Range("B2:B" & i)
For j = 2 To i
Range("A" & j).Formula = "=COUNTIF(" & rng & ",B" & i & ")"
.Value = .Value
If Range("A" & j) > 1 Then Rows(j).Interior.ColorIndex = 27
Next j
Columns(1).Delete


what could be the issue ?
 
Last edited:
Upvote 0
That's me not paying attention or testing the code! Try:
Code:
Sub FindDupRows()

Dim i As Long, j As Long
Dim rng As Range

Application.ScreenUpdating = False

Cells.Interior.ColorIndex = xlNone

i = Range("A" & Rows.Count).End(xlUp).Row
With Range("A2:A" & i)
    .FormulaR1C1 = "=RC[1]&RC[2]&RC[3]&RC[4]"
    .Value = .Value
End With

Columns(1).Insert
Set rng = Range("B2:B" & i)
For j = 2 To i
    With Range("A" & j)
      .Formula = "=COUNTIF(" & rng & ",B" & i & ")"
      .Value = .Value
      If .Value > 1 Then Rows(j).Interior.ColorIndex = 27
    End With
Next j
Columns(1).Delete

Application.ScreenUpdating = True

End Sub
See if that works
 
Upvote 0
Change that line to:
Code:
.Formula = "=COUNTIF(B2:B" & i & ",B" & j & ")"
 
Upvote 0
Thanks, the code is free of errors, but it doesnt seem to work as per the requirement, its actually not highlighting the duplicates instead its doing it randomly.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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