Match two or more rows values based on their ID in another cell

aayaanmayank

Board Regular
Joined
Jul 20, 2018
Messages
157

Hi, Can any one help me in this, i have a column contains ID and another column contains company names.

i have to first check that how many cells or times one/same id contains and then match and color code (if matches then green else yellow) company name with the similar times.
Below is the data-
[TABLE="width: 100, align: left"]
<tbody>[TR]
[TD]AE36630GU[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]PHARMACEUTICAL INDUSTRIES[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]AE36630GU[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]PHARMACEUTICAL INDUSTRIES[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]ABC INVEST[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]ABC INVEST SH.P.K (L.T.D.)[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]ABC INVEST SHPK[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALK43128401L[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]INVEST PETROLEUM ALBANIA LTD[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALK43128401L[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]INVEST PETROLEUM ALBANIA LTD[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALK82418662C[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]RAH HYDROPOWER[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALK82418662C[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]RAH HYDROPOWER SH.A.[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 89"]
<tbody>[TR]
[TD="class: xl65"]ALK82418662C[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 204"]
<tbody>[TR]
[TD="class: xl65"]RAH Hydropower Sh.A.[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 293"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
so far i have prepared a macro which is moving row by row but that only matches company names with previous row

Set SHGROUP = ThisWorkbook.Worksheets("Data_DQ")


Range("F2").Select
ActiveCell.Interior.Color = vbYellow


For U = 2 To LastRow1


Set MYNAME2 = Cells(U, "f")
Set MYNAME3 = Cells((U + 1), ("f"))


If MYNAME2.Value = MYNAME3 Then


SHGROUP.Cells(U, "f").Interior.Color = vbGreen
SHGROUP.Cells(U + 1, "f").Interior.Color = vbGreen
Else


SHGROUP.Cells(U + 1, "f").Interior.Color = vbYellow


End If
Next U
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi

Please try with below macro

Code:
Sub ColourMatch()

Dim SHGROUP  As Worksheet
Dim Temp As String
Dim I As String
Dim Lrow As String
Set SHGROUP = ThisWorkbook.Worksheets("Sheet2")
Range("A1").Select 'Change Cell value
I = "A$1:" 'mention first cellvalue range

Do While ActiveCell.Value <> ""

Temp = WorksheetFunction.CountIf(Range(I & ActiveCell.Address), Range(ActiveCell.Address))

If Temp > 1 Then

ActiveCell.Interior.Color = vbGreen

Else

ActiveCell.Interior.Color = vbYellow

End If

ActiveCell.Offset(1, 0).Select
Loop

End Sub
 
Upvote 0
Thanks but it is not working. it is just doing color coding yellow & then green color on IDs. what i need first it has to check that how many times that id exists then based on id count it has to match name rows with similar nunber of times.
e.g. US127473334L id exits 3 times in columns A so then it sud go to column B and match 1st row company name with next two rows/ hope it makes sense.
 
Upvote 0
Hi,

Let's take example ID "ALJ61924662T", has 3 time and company name are different, Please advise if company name match what should do and if now what should do ?

[TABLE="class: cms_table, width: 100, align: left"]
<tbody>[TR]
[TD][TABLE="class: cms_table, width: 89"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[TD][TABLE="class: cms_table, width: 204"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ABC INVEST[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
[TR]
[TD][TABLE="class: cms_table, width: 89"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[TD][TABLE="class: cms_table, width: 204"]
<tbody>[TR]
[TD="class: cms_table_xl65"][TABLE="class: cms_table, width: 204"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ABC INVEST SH.P.K (L.T.D.)[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
[TR]
[TD][TABLE="class: cms_table, width: 89"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ALJ61924662T[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[TD][TABLE="class: cms_table, width: 204"]
<tbody>[TR]
[TD="class: cms_table_xl65"]ABC INVEST SHPK[/TD]
[/TR]
</tbody>[/TABLE]

[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
How about
Code:
Sub HiliteCells()
   Dim cl As Range
   Dim Dic As Object
   
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = vbTextCompare
   For Each cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Not Dic.exists(cl.Value) Then
         Dic.Add cl.Value, CreateObject("scripting.dictionary")
         Dic(cl.Value).Add cl.Offset(, 1).Value, cl.Offset(, 1)
         cl.Offset(, 1).Interior.Color = vbYellow
      ElseIf Not Dic(cl.Value).exists(cl.Offset(, 1).Value) Then
         Dic(cl.Value).Add cl.Offset(, 1).Value, cl.Offset(, 1)
         cl.Offset(, 1).Interior.Color = vbYellow
      Else
         cl.Offset(, 1).Interior.Color = vbGreen
         Dic(cl.Value)(cl.Offset(, 1).Value).Interior.Color = vbGreen
      End If
   Next cl
End Sub
 
Upvote 0
Hi Could you please advise that why is it not matching below ones. it is doing yellow color in all the cells however last 3 cells has same names

[TABLE="width: 687"]
<tbody>[TR]
[TD]ALK82418002C[/TD]
[TD]DEVOLL HYDROPOWER[/TD]
[/TR]
[TR]
[TD]ALK82418002C[/TD]
[TD]DEVOLL HYDROPOWER SH.A.[/TD]
[/TR]
[TR]
[TD]ALK82418002C[/TD]
[TD]Devoll Hydropower Sh.A.[/TD]
[/TR]
[TR]
[TD]ALK82418002C[/TD]
[TD]DEVOLL HYDROPOWER SHA[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
They are yellow because not all names for that ID match.
 
Upvote 0
Thanks for clarity.. can you advise is there any possibility if i have to add that if 80% string matches then it sud give green color.
 
Upvote 0
To do that properly would require Fuzzy Logic, which is beyond my abilities.
However this will consider a match if the 1st 15 characters are the same.
Code:
Sub HiliteCells()
   Dim Cl As Range
   Dim Dic As Object
   Dim V2 As String
   
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = vbTextCompare
   For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
      V2 = Left(Cl.Offset(, 1).Value, [COLOR=#ff0000]15[/COLOR])
      If Not Dic.exists(Cl.Value) Then
         Dic.Add Cl.Value, CreateObject("scripting.dictionary")
         Dic(Cl.Value).comparemode = vbTextCompare
         Dic(Cl.Value).Add V2, Cl.Offset(, 1)
         Debug.Print Dic(Cl.Value)(Left(Cl.Offset(, 1).Value, Len(Cl.Offset(, 1).Value) - 0.2 * Len(Cl.Offset(, 1).Value)))
         Cl.Offset(, 1).Interior.Color = vbYellow
      ElseIf Not Dic(Cl.Value).exists(V2) Then
         Dic(Cl.Value).Add V2, Cl.Offset(, 1)
         Cl.Offset(, 1).Interior.Color = vbYellow
      Else
         Cl.Offset(, 1).Interior.Color = vbGreen
         Dic(Cl.Value)(V2).Interior.Color = vbGreen
      End If
   Next Cl
End Sub
Change the value in red for how many characters you want to consider
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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