Hello, I have some tool to work to comprae cuplicates and mark if exist by the one cell.
Now I need compare workbook from 2 column (creating string from 2 column)
How it should be work:
If I wrote in A = nothing
If I wrote in A and B smg it should automatickly find duplicates in workbook and mark on color.
also I'd like to looking for duplicat in for 2 ranges in worksheets in all workbook.
Sheet1:AB
Sheet1 PR
Sheet2:AB
Sheet2 PR
and so on:
first loop starts looking for match in AB then PR , AB , PR... the same value.
That's the one tool from 1 column what i use from workbook.
Also usefull will if it separete if it was founding in activesheet or othersheet.
1st code it's not sorking for duplicates in activesheet
2nd code (marks on 3 colors depends of scenerio.
Now I need compare workbook from 2 column (creating string from 2 column)
How it should be work:
If I wrote in A = nothing
If I wrote in A and B smg it should automatickly find duplicates in workbook and mark on color.
also I'd like to looking for duplicat in for 2 ranges in worksheets in all workbook.
Sheet1:AB
Sheet1 PR
Sheet2:AB
Sheet2 PR
and so on:
first loop starts looking for match in AB then PR , AB , PR... the same value.
That's the one tool from 1 column what i use from workbook.
Also usefull will if it separete if it was founding in activesheet or othersheet.
1st code it's not sorking for duplicates in activesheet
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim lRow As Long, wsLRow As Long, i As Long
Dim aCell As Range
Dim ws As Worksheet
Dim strSearch As String
With Sh
'~~> Get last row in Col A of the sheet
'~~> which got activated
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
'~~> Remove existing Color from the column
'~~> This is to cater for any deletions in the
'~~> other sheets so that cells can be re-colored
.Columns(1).Interior.ColorIndex = xlNone
'~~> Loop through the cells of the sheet which
'~~> got activated
For i = 1 To lRow
'~~> Store the ID in a variable
strSearch = .Range("A" & i).Value
if strSearch <> "" then 'eliminated color empty cell
'~~> loop through the worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
'~~> This is to ensure that it doesn't
'~~> search itself
If ws.Name <> Sh.Name Then
'~~> Get last row in Col A of the sheet
wsLRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
'~~> Use .Find to quick check for the duplicate
Set aCell = ws.Range("A1:A" & wsLRow).Find(What:=strSearch, _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'~~> If found then color the cell red and exit the loop
'~~> No point searching rest of the sheets
If Not aCell Is Nothing Then
Sh.Range("A" & i).Interior.ColorIndex = 3
Exit For
End If
End If
Next ws
End if
Next i
End With
End Sub
2nd code (marks on 3 colors depends of scenerio.
Code:
'code created by user3598756
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim IDsRng As Range, IDCell As Range
Dim ws As Worksheet
Dim strSearch As String
Dim foundInOtherSheet As Boolean, foundInActiveSheet As Boolean
With Sh
Set IDsRng = .Range("A1", .Cells(.Rows.count, 1).End(xlUp)) '<--| set the IDs range as all column A not empty cells with some "text" content
'~~> Remove existing Color from the column
'~~> This is to cater for any deletions in the other sheets so that cells can be re-colored
.Columns(1).Interior.ColorIndex = xlNone
End With
For Each IDCell In IDsRng '<--| Loop through ID cells (i.e. column A "text" cells of the activated sheet)
'~~> Store the ID in a variable
strSearch = IDCell.Value
foundInActiveSheet = WorksheetFunction.CountIf(IDsRng, strSearch) > 1 '<--| count possible dupes in active sheet
foundInOtherSheet = False '<--| initialize it at every new ID
'~~> loop through the worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
'~~> This is to ensure that it doesn't search itself
If ws.Name <> Sh.Name Then
With ws
foundInOtherSheet = WorksheetFunction.CountIf(.Range("A1", .Cells(.Rows.count, 1).End(xlUp)), strSearch) > 1
If foundInOtherSheet Then Exit For '~~> If found then color then no point searching rest of the sheets
End With
End If
Next
Select Case True '<--| now act accordingly to where duplicates have been found
Case foundInOtherSheet And Not foundInActiveSheet '<--| if duplicates found in "other" sheets only
IDCell.Interior.ColorIndex = 3 '<--| red
Case foundInOtherSheet And foundInActiveSheet '<--| if duplicates found in "other" sheets and in "active" one too
IDCell.Interior.ColorIndex = 6 '<--| yellow
Case Not foundInOtherSheet And foundInActiveSheet '<--| if duplicates found in "active" sheets only
IDCell.Interior.ColorIndex = 14 '<--| green
End Select
Next
End Sub