Vba to find duplicates in a cell

Chefsohail

Board Regular
Joined
Oct 3, 2020
Messages
90
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

I need your expertise with a code that can highlight all cells in a row if the cell contains any duplicate statement. The duplication has to be within the cell.

Eg:
1606626680776.png


I am not sure if this is practically possible in excel. In case if it isn't at least the below alternative will help.

Alternative: A code that can highlight all cells in a row if the cell contains any duplicate words except for articles, prepositions and conjunctions. The duplication has to be within the cell.

1606626267603.png


I hope the examples above illustrates the requirement. I do not need the remarks column. Only the cell needs to be highlighted.
 
Try this version

VBA Code:
Sub Highlight_Dupes_Min_Letters_v2()
  Dim RX As Object, d As Object
  Dim i As Long
  Dim a As Variant, itm As Variant
  Dim s As String, Pat1 As String, Pat2 As String
 
  Const MinLetters As Long = 2  '<- Edit as required
  Const IgnoreWords As String = "is|the|an|a|while|wherever|where|whenever|when|though|that|than|so that|since|only|once|lest|if|even|because|as|although" _
                                & "|so|yet|or|but|Nor|And|For|up|under|toward|to|till|through|over|outside|of|onto|on|off|next to|near|into|inside|in|from" _
                                & "|for|during|down|by|between|beside|beneath|below|behind|before|away from|at|around|among|along|against|after|across|above"
 
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  Set RX = CreateObject("VBScript.RegExP")
  RX.Global = True
  Pat1 = "\b(" & IgnoreWords & ")\b"
  Pat2 = "\b[^ ]{" & MinLetters & ",}\b"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      d.RemoveAll
      s = a(i, 1)
      RX.Pattern = Pat1
      s = RX.Replace(s, "")
      RX.Pattern = Pat2
      For Each itm In RX.Execute(s)
        If d.exists(CStr(itm)) Then
          .Cells(i).Interior.Color = vbYellow
          Exit For
        Else
          d(CStr(itm)) = 1
        End If
      Next itm
    Next i
  End With
End Sub

My small sample data and results:

Chefsohail.xlsm
A
1Data
2elephant next to rhino next to hippo
3book paper book
4books paper books
5the cat sat on the mat
6one elephant chased another elephant
7red train, red car, blue train
8M.M Cyprus
9Peter is excellent at macro codes. He is a master in Excel and VBA
10Peter is hopeless at cooking and hopeless as a debater
Sheet2
Awesome.. Thanx Peter...
Will try and test.

Do you conduct macro classes?

Will you teach me? My Macro Guru.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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