VBA to Compare 2 sheets and count and color color similarities

jskasango

Board Regular
Joined
Jul 18, 2012
Messages
202
Office Version
  1. 365
Platform
  1. Windows
Hi ALL, I have an assignment to compare all of Sheet1 contents with Sheet2 contents (more than 30,000 rows) then count and color similarities in Sheet1.
Both sheets have a header row and I want the counts to be inserted at row2 of each column in sheet1 when the search ends.
Some contents in sheet1 appear more than once and each find should be treated as a new find.
Please help with a VBA code that I can use.
TIA
K
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You have contents on each sheet in rows but you want to counts to be inserted at row 2?

You will get count for each sample in Sheet1 when comparing to Sheet2. How you want to put this?

I think create posting few lines of examples and how you want the final result to be presented will help others to understand better.
 
Upvote 0
sampleData.xls
ABCD
1JINAKWAOANWANIKUTOKA
2KAS 138 YMwanzoSiriJina
3KBA 043 PMwanzoSiriJina
4KAZ 616 CMwanzoSiriJina
5KBJ 078 GOriginsaraJina
6KBE 060 GOriginsaraFdfd
7KBD 419 NOriginsaradsFD
8KBP 075VOriginsaraJina
9KBP 077VMwanzosaraJina
10KAZ 616 CMwanzoSiriJina
11KBJ 078 GOriginbenJina
12KBE 060 GMwanzobensdasdasd
13KBD 419 NMwanzobenJina
14KBP 075VMwanzobenJina
15 Thomas nganga KasbenJina
16 Scholastica kasango MwanzoSiriJina
17Emelda nzau fsdfSFSiriJina
18Blandina nthenya MwanzoSiriJina
candidates


What I need is a VBA code that will run through the candidates and check in the masterfile and come up with two sheets "FOUND" and "NOT FOUND" copying the entire row of the candidates sheet.
 
Upvote 0
sampleData.xls
ABCD
1JINAKWAOANWANIKUTOKA
2KAS 138 YMwanzoSiriJina
3KBA 043 PMwanzoSiriJina
4KAZ 616 CMwanzoSiriJina
5KBJ 078 GOriginsaraJina
6KBE 060 GOriginsaraFdfd
7KBD 419 NOriginsaradsFD
8KBP 075VOriginsaraJina
9KBP 077VMwanzosaraJina
10KAZ 616 CMwanzoSiriJina
11KBJ 078 GOriginbenJina
12KBE 060 GMwanzobensdasdasd
13KBD 419 NMwanzobenJina
14KBP 075VMwanzobenJina
15 Thomas nganga KasbenJina
16 Scholastica kasango MwanzoSiriJina
17Emelda nzau fsdfSFSiriJina
18Blandina nthenya MwanzoSiriJina
candidates


What I need is a VBA code that will run through the candidates and check in the masterfile and come up with two sheets "FOUND" and "NOT FOUND" copying the entire row of the candidates sheet.
It is not cleat still.

Copy to where? Another sheet?

You mentioned that the content may appear more than one. By content, you meant just column A or the whole column A to D duplicated?

If duplicated, then what to do? Ignore? You also want to count how many duplication and insert in 2nd row? You have already copy the matching into result sheet (I presumed) and you have a list of matching contents, how do I put in 2nd row? This is why you need to show how your output looks like.
 
Upvote 0
Thank you for your quick response. The search column is A. Also, the search column in MasterSheet is A. If found the entire row should be copied from Sheet "Candidates" to Sheet "Found", if not found, the entire row should be copied from Sheet "Candidates" to Sheet "NotFound".
The outputs should look like this:-
1654845969097.png
1654846038756.png

These two snippets are just an example of what I wish to achieve. I have done them manually so they are not accurate. Duplicates are allowed.
 
Upvote 0
I see that you cannot just compare column A because other columns can be different. So, I'm comparing all columns

Try this
VBA Code:
Sub Segregate()

Dim strData As String
Dim n As Long
Dim cell As Range, rngData As Range, rngMaster As Range
Dim dictData As Object
Dim wsMaster As Worksheet, wsCand As Worksheet, wsFound As Worksheet, wsNotFound As Worksheet

Set wsMaster = ActiveWorkbook.Sheets("Master")
Set wsCand = ActiveWorkbook.Sheets("Candidates")
Set wsFound = ActiveWorkbook.Sheets("Found")
Set wsNotFound = ActiveWorkbook.Sheets("NotFound")

Set dictData = CreateObject("Scripting.Dictionary")
Set rngData = wsCand.Range("A2", wsCand.Cells(Rows.Count, "A").End(xlUp))
Set rngMaster = wsMaster.Range("A2", wsMaster.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngMaster
    strData = cell & cell.Offset(, 1) & cell.Offset(, 2) & cell.Offset(, 3)
    If Not dictData.Exists(strData) Then dictData.Add strData, Nothing
Next
    
For Each cell In rngData
    strData = cell & cell.Offset(, 1) & cell.Offset(, 2) & cell.Offset(, 3)
    If dictData.Exists(strData) Then
        n = wsFound.Range("A" & Rows.Count).End(xlUp).Row + 1
        wsCand.Range("A" & cell.Row, "D" & cell.Row).Copy wsFound.Range("A" & n)
    Else
        n = wsNotFound.Range("A" & Rows.Count).End(xlUp).Row + 1
        wsCand.Range("A" & cell.Row, "D" & cell.Row).Copy wsNotFound.Range("A" & n)
    End If
Next

End Sub
 
Upvote 0
I see that you cannot just compare column A because other columns can be different. So, I'm comparing all columns

Try this
VBA Code:
Sub Segregate()

Dim strData As String
Dim n As Long
Dim cell As Range, rngData As Range, rngMaster As Range
Dim dictData As Object
Dim wsMaster As Worksheet, wsCand As Worksheet, wsFound As Worksheet, wsNotFound As Worksheet

Set wsMaster = ActiveWorkbook.Sheets("Master")
Set wsCand = ActiveWorkbook.Sheets("Candidates")
Set wsFound = ActiveWorkbook.Sheets("Found")
Set wsNotFound = ActiveWorkbook.Sheets("NotFound")

Set dictData = CreateObject("Scripting.Dictionary")
Set rngData = wsCand.Range("A2", wsCand.Cells(Rows.Count, "A").End(xlUp))
Set rngMaster = wsMaster.Range("A2", wsMaster.Cells(Rows.Count, "A").End(xlUp))

For Each cell In rngMaster
    strData = cell & cell.Offset(, 1) & cell.Offset(, 2) & cell.Offset(, 3)
    If Not dictData.Exists(strData) Then dictData.Add strData, Nothing
Next
   
For Each cell In rngData
    strData = cell & cell.Offset(, 1) & cell.Offset(, 2) & cell.Offset(, 3)
    If dictData.Exists(strData) Then
        n = wsFound.Range("A" & Rows.Count).End(xlUp).Row + 1
        wsCand.Range("A" & cell.Row, "D" & cell.Row).Copy wsFound.Range("A" & n)
    Else
        n = wsNotFound.Range("A" & Rows.Count).End(xlUp).Row + 1
        wsCand.Range("A" & cell.Row, "D" & cell.Row).Copy wsNotFound.Range("A" & n)
    End If
Next

End Sub
Let me test it. My Master file has only one column. I believe it will work.
 
Upvote 0
The search should be only on column A of Candidates and Master, if found or NOT found, copy entire row to the appropriate sheet (ie Sheet(FOUND) or Sheet(NOTFOUND). thank you very much for your efforts.
 
Upvote 0
The search should be only on column A of Candidates and Master, if found or NOT found, copy entire row to the appropriate sheet (ie Sheet(FOUND) or Sheet(NOTFOUND). thank you very much for your efforts.
Let's see if I understood you right.

You are just comparing column A in both Candidates and Master list. You want to go through Candidates list and if there is match in Master, you want you copy the whole matched row on Candidates to Found sheet, otherwise just copy the whole row into NotFound sheet. Is this correct?

I found that there are similar strings in Master (column A) such as KBE 060 G which appears on row 6 and row 11, but the rest contents on their corresponding rows are not the same. So, that was why I compared all the contents instead of just column A. If you use LookUp or Find functions, it will match twice on row 6 and 11 and matched Candidates row will be copied twice on Found sheet. Please clarify.
 
Upvote 0
Let's see if I understood you right.

You are just comparing column A in both Candidates and Master list. You want to go through Candidates list and if there is match in Master, you want you copy the whole matched row on Candidates to Found sheet, otherwise just copy the whole row into NotFound sheet. Is this correct?

I found that there are similar strings in Master (column A) such as KBE 060 G which appears on row 6 and row 11, but the rest contents on their corresponding rows are not the same. So, that was why I compared all the contents instead of just column A. If you use LookUp or Find functions, it will always matched on row 6. The row 11 would be useless data I suppose. Please clarify
You are very correct. But what is in row 11 is not useless. Duplicates are allowed.
 
Upvote 0

Forum statistics

Threads
1,215,361
Messages
6,124,500
Members
449,166
Latest member
hokjock

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