Compare two cell and do green color if 80% text matches

aayaanmayank

Board Regular
Joined
Jul 20, 2018
Messages
157
Hi Can any one help me on this? i have below data which need to compare with the next row and give green color if 80% text matches

image removed

i have written below code but excel is getting hang whenever i run the Macro

sub colorr()

Set shgroup = ThisWorkbook.Worksheets("Grouping Data_Underwriters")


lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row


Range("B2").Select


For U = 2 To lastrow1

Set MYNAME2 = Cells(U, "B")
Set MYNAME3 = Cells((U + 1), ("B"))
MY = Left(MYNAME2, Len(MYNAME2) * 0.8)

x = Len(MY)
y = Left(MYNAME3, x)
If y = MY Then


shgroup.Cells(U, "B").Interior.Color = vbGreen
shgroup.Cells(U + 1, "B").Interior.Color = vbGreen


End If


Next U

END SUB
 
Last edited by a moderator:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Could do with conditional formatting :

=LEFT(A1,LEN(A1)*0.8)=LEFT(A2,LEN(LEFT(A1,LEN(A1)*0.8)))
 
Upvote 0
Your macro ran OK for me but don't know whether it does what you want :
Code:
Sub colorr()
Dim shgroup As Worksheet, lastrow1%, U%, MYNAME2 As Range, MYNAME3 As Range, x%, y$, MY$
Set shgroup = ThisWorkbook.Worksheets("Grouping Data_Underwriters")
lastrow1 = shgroup.Range("B" & Rows.Count).End(xlUp).Row
Range("B2").Select
For U = 2 To lastrow1
    Set MYNAME2 = Cells(U, "B")
    Set MYNAME3 = Cells((U + 1), ("B"))
    MY = Left(MYNAME2, Len(MYNAME2) * 0.8)
    x = Len(MY)
    y = Left(MYNAME3, x)
    If y = MY Then
        shgroup.Cells(U, "B").Interior.Color = vbGreen
        shgroup.Cells(U + 1, "B").Interior.Color = vbGreen
    End If
Next U
End Sub
 
Upvote 0
Here's some alternative code :
Code:
Sub colorr()
Dim rng As Range, cel As Range
With Worksheets("Grouping Data_Underwriters")
    Set rng = .Range([B2], .Range("B" & Rows.Count).End(xlUp))
End With
For Each cel In rng
    If Left(cel, Len(cel) * 0.8) = Left(cel(2), Len(Left(cel, Len(cel) * 0.8))) Then _
    cel.Resize(2).Interior.Color = vbGreen
Next
End Sub
 
Upvote 0
Yes its solving my purpose thanks alot, however can you advise why is it doing green color on below name even text is not matching 80% text
[TABLE="width: 235"]
<colgroup><col></colgroup><tbody>[TR]
[TD]JOHNSONNOIL[/TD]
[/TR]
[TR]
[TD]JOHNSONNOILANUNITEDGENERALPARTNERSHIP[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Just want to understand and if you can check that why is it doing green color on below name even text is not matching 80% text
[TABLE="class: cms_table, width: 235"]
<tbody>[TR]
[TD]JOHNSONNOIL[/TD]
[/TR]
[TR]
[TD]JOHNSONNOILANUNITEDGENERALPARTNERSHIP[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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