Return cells only containing certain letters

oaj1977

New Member
Joined
Nov 15, 2020
Messages
8
Office Version
  1. 2016
Platform
  1. MacOS
Here’s my scenario:

I have a list of words and a list of letters.
I want to return the words that contain letters from the list of letters and ONLY from the list of letters. For example:

WORDS: cog, bib, ab, back, cab
LETTERS: A, B, C
WORDS returned: ab, cab

Thoughts?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What if there are repeated letters? For example, say your letters are B, C, I... would the word BIB be returned to you or not?
 
Upvote 0
Assuming the answer to Rick's question is yes, here's a UDF you can try. Assumes the list of words and the list of allowed letters are ", "-delimited strings and you don't want to employ case sensitivity.
Book1
ABCD
1cog, bib, ab, back, cabab, cabAllowed Letters
2bib, ibb, jkl, mno A, B, C
3aCB, BBB, BBA, aab, cccaCB, BBB, BBA, aab, ccc
4 
Sheet2
Cell Formulas
RangeFormula
B1:B4B1=oaj(A1,$D$2)

VBA Code:
Function oaj(Words As String, Allowed As String) As String
'Assumes delimiter is always ", " for both Words and Allowed
Dim Vwrds As Variant, Vallow As Variant, i As Long, j As Long, k As Long, Ct As Long, Out As Variant
Vwrds = Split(Words, ", ")
Vallow = Split(Allowed, ", ")
For i = LBound(Vwrds) To UBound(Vwrds)
    Ct = 0
    For j = 1 To Len(Vwrds(i))
        For k = LBound(Vallow) To UBound(Vallow)
            If UCase(Mid(Vwrds(i), j, 1)) = UCase(Vallow(k)) Then
                Ct = Ct + 1
                If Ct = Len(Vwrds(i)) Then
                    Out = Out & ", " & Vwrds(i)
                    GoTo Nx
                End If
            End If
        Next k
    Next j
Nx:
Next i
If Out <> "" Then
    oaj = Mid(Out, 3, Len(Out))
Else
    oaj = ""
End If
End Function
 
Upvote 0
Welcome to the MrExcel board!

Here is an alternative user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function myWords(s As String, letters As String) As String
  Dim RX As Object, itm As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  RX.Pattern = "(\b[" & letters & "]+\b)"
  For Each itm In RX.Execute(s)
    myWords = myWords & IIf(myWords = vbNullString, "", ", ") & itm
  Next itm
End Function

oaj1977.xlsm
ABCD
1WordsResultsAllowed Letters
2cog, bib, ab, back, cabab, cabABC
3bib, ibb, jkl, mno 
4aCB, BBB, BBA, aab, cccaCB, BBB, BBA, aab, ccc
5 
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=myWords(A2,D$2)
 
Upvote 0
What if there are repeated letters? For example, say your letters are B, C, I... would the word BIB be returned to you or not?
Yes - repeated letters would be returned.
Welcome to the MrExcel board!

Here is an alternative user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function myWords(s As String, letters As String) As String
  Dim RX As Object, itm As Object

  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  RX.Pattern = "(\b[" & letters & "]+\b)"
  For Each itm In RX.Execute(s)
    myWords = myWords & IIf(myWords = vbNullString, "", ", ") & itm
  Next itm
End Function

oaj1977.xlsm
ABCD
1WordsResultsAllowed Letters
2cog, bib, ab, back, cabab, cabABC
3bib, ibb, jkl, mno 
4aCB, BBB, BBA, aab, cccaCB, BBB, BBA, aab, ccc
5 
Sheet1
Cell Formulas
RangeFormula
B2:B5B2=myWords(A2,D$2)
so the list of words will be in individual cells and be on the magnitude of tens of thousands.
 
Upvote 0
Assuming the answer to Rick's question is yes, here's a UDF you can try. Assumes the list of words and the list of allowed letters are ", "-delimited strings and you don't want to employ case sensitivity.
Book1
ABCD
1cog, bib, ab, back, cabab, cabAllowed Letters
2bib, ibb, jkl, mno A, B, C
3aCB, BBB, BBA, aab, cccaCB, BBB, BBA, aab, ccc
4 
Sheet2
Cell Formulas
RangeFormula
B1:B4B1=oaj(A1,$D$2)

VBA Code:
Function oaj(Words As String, Allowed As String) As String
'Assumes delimiter is always ", " for both Words and Allowed
Dim Vwrds As Variant, Vallow As Variant, i As Long, j As Long, k As Long, Ct As Long, Out As Variant
Vwrds = Split(Words, ", ")
Vallow = Split(Allowed, ", ")
For i = LBound(Vwrds) To UBound(Vwrds)
    Ct = 0
    For j = 1 To Len(Vwrds(i))
        For k = LBound(Vallow) To UBound(Vallow)
            If UCase(Mid(Vwrds(i), j, 1)) = UCase(Vallow(k)) Then
                Ct = Ct + 1
                If Ct = Len(Vwrds(i)) Then
                    Out = Out & ", " & Vwrds(i)
                    GoTo Nx
                End If
            End If
        Next k
    Next j
Nx:
Next i
If Out <> "" Then
    oaj = Mid(Out, 3, Len(Out))
Else
    oaj = ""
End If
End Function
The list of words will be in individual cells and will number in the tens of thousands. :-/
 
Upvote 0
The list of words will be in individual cells and will number in the tens of thousands. :-/
Please post a sample of your data, using XL2BB, both the list of words and the list of allowed letters so we can see the layout.
 
Upvote 0
I've been getting some compile errors with XL2BB, so here it is as a table and also attached a screenshot...

Letters:g
i
e
l
a
d
Words
aahs
aals
aani
aaru
abac
abay
abas
abba
abbe
abby
abbr
abed
abey
abel
abet
abib
abie
abye
abir
abys
abit
able
ably
abos
abow
abox
abri
absi
abut
acad
acca
acce
acct
aced
acer
aces
ache
achy
acid
acyl
acis
acle
aclu
acme
acne
acop
acor
acpt
acre
acta
actg
acts
actu
acus
adad
adai
aday
adam
adar
adat
adaw
adda
addy
addn
addr
adds
addu
aden
adet
adib
adin
adit
adjt
admi
adod
adon
ador
ados
adry
advt
adze
aeon
aery
aero
aesc
afar
afer
affa
affy
afft
afro
agad
agag
agal
agao
agar
agas
agau
agaz
agba
agcy
aged
agee
agen
ager
ages
aget
agha
agib
agin
agio
agit
agla
agly
agma
agog
agon
agos
agra
agre
agst
agua
ague
ahab
ahey
ahem
ahet
ahir
ahoy
ahom
ahum
ayah
aias
aide
aids
aiel
ayen
ayes
ayin
 

Attachments

  • Screen Shot 2020-11-16 at 10.59.50 AM.png
    Screen Shot 2020-11-16 at 10.59.50 AM.png
    82 KB · Views: 8
Upvote 0
See if this does what you want:
Book1.xlsm
AB
1Letters:g
2i
3e
4l
5a
6d
7
8
9Words
10aahs 
11aals 
12adadadad
13adaiadai
14ADAIADAI
15aDAiaDAi
16abas 
17abba 
18abbe 
19abby 
20abbr 
Sheet3
Cell Formulas
RangeFormula
B10:B20B10=Allow(A10,$B$1:$B$6)

VBA Code:
Function Allow(Words As String, Lett As Variant) As String
Dim Vallow As Variant, i As Long, j As Long, Ct As Long
Vallow = Lett.Value
For i = 1 To Len(Words)
    For j = LBound(Vallow, 1) To UBound(Vallow, 1)
        If UCase(Mid(Words, i, 1)) = UCase(Vallow(j, 1)) Then
            Ct = Ct + 1
            If Ct = Len(Words) Then
                Allow = Words
                GoTo Nx
            End If
        End If
    Next j
    Allow = ""
Nx:
Next i
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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