'Extracting repeated text in new column from phrases- compared to two columns in Excel
I have two columns ('Index 1' and 'Index 2') with keywords and I want to extract from 'Index 2' the duplicate words that are found in 'Index 1', in column 'Results'.
This is how the output should look:
| Indexed 1 | Indexed 2 | Result |
|---|---|---|
| comfort; ideal; manual | bra; comfort; manual | comfort; manual |
| shirt; basic; reservation | male; basic; female | basic |
| quality; stylish; basic; materials | stylish; female; shirt; materials | stylish; materials |
I've tried this code but it would only highlight the duplicate words from the same cell. I can't manage to adapt it to my needs, mostly because I'm new to VBA coding:
Sub HighlightDupesCaseInsensitive()
Dim cell As Range
Dim Delimiter As String
Delimiter = InputBox("Enter the delimiter that separates values in a cell", "Delimiter", ", ")
For Each cell In Application.Selection
Call HighlightDupeWordsInCell(cell, Delimiter, False)
Next
End Sub
Sub HighlightDupeWordsInCell(cell As Range, Optional Delimiter As String = " ", Optional CaseSensitive As Boolean = True)
Dim text As String
Dim words() As String
Dim word As String
Dim wordIndex, matchCount, positionInText As Integer
If CaseSensitive Then
words = Split(cell.Value, Delimiter)
Else
words = Split(LCase(cell.Value), Delimiter)
End If
For wordIndex = LBound(words) To UBound(words) - 1
word = words(wordIndex)
matchCount = 0
For nextWordIndex = wordIndex + 1 To UBound(words)
If word = words(nextWordIndex) Then
matchCount = matchCount + 1
End If
Next nextWordIndex
If matchCount > 0 Then
text = ""
For Index = LBound(words) To UBound(words)
text = text & words(Index)
If (words(Index) = word) Then
cell.Characters(Len(text) - Len(word) + 1, Len(word)).Font.Color = vbRed
End If
text = text & Delimiter
Next
End If
Next wordIndex
End Sub
Can you please help?
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
