• Status: Solved
• Priority: Low
• Security: Public
• Views: 55

# whitelist of words

opposite of this one.
https://www.experts-exchange.com/questions/29079746/blacklist-of-words-find-if-cells-have-the-value.html#a42444598

need to put a 1 value in column AA where the value in A matches a word in sheet2
0
finnstone
• 4
• 3
1 Solution

Older than dirtCommented:
Is "AA" the column where you want a "1"?

``````Sub macro()
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
Set regex = CreateObject("VBScript.RegExp")

Set rngWrds = Range(sh2.Range("A1"), sh2.Range("A" & Rows.Count).End(xlUp))
aRng = WorksheetFunction.Index(WorksheetFunction.Transpose(rngWrds), 1, 0)
strRng = Join(aRng, "|")
With regex
.Global = False
.MultiLine = False
.IgnoreCase = True
.Pattern = "\b(" & strRng & ")+\b"
End With
For Idx = sh1.Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
If regex.Test(sh1.Range("A" & Idx)) Then
sh1.Range("AA" & Idx) = "1"
End If
Next
End Sub
``````
0

Commented:
HI,

If you want the count of matched words instead of 1
``````Sub macro()
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
Set regex = CreateObject("VBScript.RegExp")

Set rngWrds = Range(sh2.Range("A1"), sh2.Range("A" & Rows.Count).End(xlUp))
aRng = WorksheetFunction.Index(WorksheetFunction.Transpose(rngWrds), 1, 0)
strRng = Join(aRng, "|")
With regex
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "\b(" & strRng & ")+\b"
End With
For Idx = sh1.Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
If regex.Test(sh1.Range("A" & Idx)) Then
set Mtchs = regex.Execute(sh1.Range("A" & Idx))
sh1.Range("AA" & Idx) = Mtchs.Count
End If
Next
End Sub
``````
Regards
0

Author Commented:
thx!
0

Older than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
Experts Exchange MVE 2015
Experts Exchange Top Expert Visual Basic Classic 2012 to 201
0

Author Commented:
THIS does not seem to be working as intended. please see attached. why are rows getting flagged as meting the white list conditions when they do not have any of the words matching the column in sheet2??!

thanks
0

Older than dirtCommented:
0

Author Commented:
look at rows 17,18,21 ...none have words that are in sheet2
0

Older than dirtCommented:
Row 17 contains 'technology'  which matches (except for case) Sheet2 row 81.
Row 18 contains 'platform'  which matches (except for case) Sheet2 row 65.
Row 21 contains 'Business'  which matches Sheet2 row 13.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.