Can anyone help bring me to the finish line with this Do While Loop?
Essentially, I've got a column in my spreadsheet that is populated with of sentences. I'm trying to evaluate every word in each cell to determine if it contains a keyword existing as an element in the keywords Array. If so, the included keywords are to be listed in the corresponding cells on the column "Keywords."
However, when keywords were mentioned twice my sub routine lists them twice, eg. "keyword, keyword." All I need to do is remove the duplicate keywords from my output
Here is what I have thus far for this somewhat expansive sub routine.
Private Sub brand_names() 'inserts column with product brand mentioned in SalesForce Case
Dim ws As Worksheet
Dim last_col As Integer
Dim PuncChars, products, x, InArray As Variant
Dim i As Long, r As Long, q As Long
Dim txt, inputstring As String
keywords = Array("KEYWORDONE","KEYWORDTWO","KEYWORDTHREE")
PuncChars = Array(".", ",", ";", ":", "'", "!", "#", _
"$", "%", "&", "(", ")", " - ", "_", "--", "+", _
"=", "~", "/", "\", "{", "}", "[", "]", """", "?", "*", "#")
Set ws = ThisWorkbook.Worksheets("Applicable Spreadsheet")
last_col = ws.Cells(1, Columns.Count).End(xlToLeft).Column + 1
ws.Cells(1, last_col).Value = "Keywords"
On Error GoTo Endproc
Application.ScreenUpdating = False
r = 2
Do While Not Cells(r, 12) = ""
' coverts to UPPERCASE
txt = UCase(Cells(r, 12))
' Remove punctuation
For i = 0 To UBound(PuncChars)
txt = Replace(txt, PuncChars(i), " ")
Next i
' Remove excess spaces
txt = WorksheetFunction.Trim(txt)
' Extract the words
x = Split(txt)
'append words to array
For i = 0 To UBound(x)
For z = 0 To UBound(keywords)
If x(i) = keywords(z) Then
ws.Cells(r, last_col).Value = ws.Cells(r, last_col).Value & x(i) & ","
End If
Next z
Next i
'code to remove duplicates would go here
r = r + 1
Loop
'need to remove duplciates from mentioned brands and add comma between each one
Endproc:
Application.ScreenUpdating = True
Exit Sub
MsgBox ("error")
End Sub
I'm trying to add a line to my Do loop that will reduce the duplicate keywords in the output cells, so that rather than "KEYWORDONE,KEYWORDONE" it just says "KEYWORDONE"
Does anyone have an idea how I can do this from within the Do loop?