VBA code to find duplicates and paste on another sheet

miraclious

New Member
Joined
Oct 10, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi I need to find duplicates across about 15 sheets in a workbook. But my code can only reach 2 sheets. Need help to change the code:

Sub GetDups()
Dim myCount As Long
Dim Rng1 As Range, Rng2 As Range, C As Range
myCount = 0
Set Rng1 = Worksheets("Sheet1").Range("A1:A100")
Set Rng2 = Worksheets("Sheet2").Range("A1:A100")
For Each C In Rng1
If WorksheetFunction.CountIf(Rng2, C) Then
myCount = WorksheetFunction.CountIf(Rng2, C) + myCount
With Worksheets("Sheet3")
.Cells(myCount, 1).Value = C
End With
End If
Next C
End Sub


Thank you
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi and welcome to MrExcel!

What is the comparison you want to make?
Sheet1 with sheet2, sheet1 with sheet3, sheet1 with sheet4 and so on up to sheet15?
Or do you want to know if there are duplicates of all against all sheets?
 
Upvote 0
If you have the following values on each sheet:
Dante Amor
A
1val1
2val2
3val3
4val4
5val5
6val6
7val7
8val8
9val9
10val10
Sheet1

Dante Amor
A
1
2val2
3
4val4
5
6val6
Sheet2

Dante Amor
A
1val3
2val6
3val9
Sheet3

Dante Amor
A
1val4
2val6
3val10
Sheet4


The result in the sheet "Results" (you must create a sheet with this name):
Dante Amor
AB
1
2val2Sheet1|Sheet2
3val3Sheet1|Sheet3
4val4Sheet1|Sheet2|Sheet4
5val6Sheet1|Sheet2|Sheet3|Sheet4
6val9Sheet1|Sheet3
7val10Sheet1|Sheet4
Results


In column A the value and in column B the sheets where it is duplicated. Values that are not duplicates simply do not appear in the result.

Try this.
VBA Code:
Sub comparison_against_all_sheets()
  Dim shR  As Worksheet, sh As Worksheet
  Dim dic As Object, ky As Variant, v As Variant
  Dim i As Long
  
  Set shR = Sheets("Results")   'Sheet with the results
  shR.Cells.ClearContents
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each sh In Sheets
    If sh.Name <> shR.Name Then
      For i = 1 To sh.Range("A" & Rows.Count).End(3).Row
        v = sh.Range("A" & i).Value
        If v <> "" Then
          If Not dic.exists(v) Then dic(v) = sh.Name Else dic(v) = dic(v) & "|" & sh.Name
        End If
      Next
    End If
  Next
  
  For Each ky In dic.keys
    If InStr(1, dic(ky), "|") > 0 Then
      shR.Range("A" & Rows.Count).End(3)(2).Resize(1, 2).Value = Array(ky, dic(ky))
    End If
  Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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