Hi All,
I have several workbooks that are 100 worksheets or so and I would like to be able to have a macro that searches for duplicate data across worksheets and generates a list of the offending data values, along with their cell addresses. I have the following code from thread707-1379708 that WinblowsME came up with and I slightly modified. The problem with the code is that it seems to only search a single column on a single worksheet. I need it to search all columns on all worksheets to match "data1" which might be on sheet 1 column B with "data1" which might be on sheet 87 column D. Thanks for any input you may be able to muster!
-PugnaxX
Here's the code:
Sub Print_Duplicates()
Dim duplicates() As String, item As Variant, last_row As Long
'The following inserts a new column to the left of the existing A column for the list of duplicates to reside &
' selects the cell "A1" ("A1" is the "ActiveCell" now)
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
Range("A1").Value = ("DUPES")
Range("A2").Select
'I think this figures out what the last row with data is
last_row = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
'This calls the function that finds the duplicates
Call Get_Duplicates(duplicates, 1, 2, last_row)
'This is my main modification, it sets the activecell's value to the duplicate item then moves down a row and sets
' that one equal to the next duplicate, ultimately creating a list of all duplicate values in your active worksheet
For Each item In duplicates
ActiveCell.Value = item
ActiveCell.Offset(1, 0).Select
Next
End Sub
Private Sub Get_Duplicates(ByRef duplicates() As String, first_row As Long, first_col As Long, last_row As Long)
'This is all code that I found on tek-tips.com, I'm not sure that I can decipher it line by line, but
'from what I can tell it is basically setting up a "dictionary" of all values in a column and then checking
'it for dupes
Dim i As Long, k As Long, curr_cell As String
Dim obj_all As Object, obj_duplicates As Object
Set obj_all = CreateObject("Scripting.Dictionary")
Set obj_duplicates = CreateObject("Scripting.Dictionary")
k = 0
For i = first_row To last_row
curr_cell = Trim(Cells(i, first_col))
If obj_all.exists(curr_cell) Then
If Not obj_duplicates.exists(curr_cell) Then
ReDim Preserve duplicates(k)
duplicates(k) = curr_cell
k = k + 1
obj_duplicates.Add curr_cell, ""
End If
Else
obj_all.Add curr_cell, ""
End If
Next i
obj_duplicates.RemoveAll
obj_all.RemoveAll
Set obj_duplicates = Nothing
Set obj_all = Nothing
End Sub
I have several workbooks that are 100 worksheets or so and I would like to be able to have a macro that searches for duplicate data across worksheets and generates a list of the offending data values, along with their cell addresses. I have the following code from thread707-1379708 that WinblowsME came up with and I slightly modified. The problem with the code is that it seems to only search a single column on a single worksheet. I need it to search all columns on all worksheets to match "data1" which might be on sheet 1 column B with "data1" which might be on sheet 87 column D. Thanks for any input you may be able to muster!
-PugnaxX
Here's the code:
Sub Print_Duplicates()
Dim duplicates() As String, item As Variant, last_row As Long
'The following inserts a new column to the left of the existing A column for the list of duplicates to reside &
' selects the cell "A1" ("A1" is the "ActiveCell" now)
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Range("A1").Select
Range("A1").Value = ("DUPES")
Range("A2").Select
'I think this figures out what the last row with data is
last_row = ActiveSheet.Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
'This calls the function that finds the duplicates
Call Get_Duplicates(duplicates, 1, 2, last_row)
'This is my main modification, it sets the activecell's value to the duplicate item then moves down a row and sets
' that one equal to the next duplicate, ultimately creating a list of all duplicate values in your active worksheet
For Each item In duplicates
ActiveCell.Value = item
ActiveCell.Offset(1, 0).Select
Next
End Sub
Private Sub Get_Duplicates(ByRef duplicates() As String, first_row As Long, first_col As Long, last_row As Long)
'This is all code that I found on tek-tips.com, I'm not sure that I can decipher it line by line, but
'from what I can tell it is basically setting up a "dictionary" of all values in a column and then checking
'it for dupes
Dim i As Long, k As Long, curr_cell As String
Dim obj_all As Object, obj_duplicates As Object
Set obj_all = CreateObject("Scripting.Dictionary")
Set obj_duplicates = CreateObject("Scripting.Dictionary")
k = 0
For i = first_row To last_row
curr_cell = Trim(Cells(i, first_col))
If obj_all.exists(curr_cell) Then
If Not obj_duplicates.exists(curr_cell) Then
ReDim Preserve duplicates(k)
duplicates(k) = curr_cell
k = k + 1
obj_duplicates.Add curr_cell, ""
End If
Else
obj_all.Add curr_cell, ""
End If
Next i
obj_duplicates.RemoveAll
obj_all.RemoveAll
Set obj_duplicates = Nothing
Set obj_all = Nothing
End Sub