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?
 
Here is another way to write the Allow function (call it the same way you call Joe's version....
VBA Code:
Function Allow(Word As String, Lett As Variant) As String
  Dim Vallow As String
  Vallow = Join(Application.Transpose(Lett), "")
  If Evaluate(Replace("SUM(0+ISNUMBER(SEARCH(MID(""@"",ROW(1:" & Len(Word) & "),1),""" & Vallow & """)))=LEN(""@"")", "@", Word)) Then Allow = Word
End Function
 
Last edited:
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Here is another way to write the Allow function (call it the same way you call Joe's version....
VBA Code:
Function Allow(Word As String, Lett As Variant) As String
  Dim Vallow As String
  Vallow = Join(Application.Transpose(Lett), "")
  If Evaluate(Replace("SUM(0+ISNUMBER(SEARCH(MID(""@"",ROW($1:$4),1),""" & Vallow & """)))=LEN(""@"")", "@", Word)) Then Allow = Word
End Function
Actually, we can write this as a one-liner...
VBA Code:
Function Allow(Word As String, Lett As Variant) As String
  If Evaluate(Replace("SUM(0+ISNUMBER(SEARCH(MID(""@"",ROW(1:" & Len(Word) & "),1),""" & Join(Application.Transpose(Lett), "") & """)))=LEN(""@"")", "@", Word)) Then Allow = Word
End Function
 
Upvote 0
here it is as a table and also attached a screenshot...
Always best to include the expected results so that we can be sure of what they are and where you want them.
I am imagining that you would want the results in a compressed list like below and with tens of thousands of words I think a UDF might be a bit lethargic so you could see if this is any use.
You may have to amend this code to match your layout as I have assumed the words are in column A starting in row 2 and the letters are in column D starting in row 1.

VBA Code:
Sub Get_Allowed()
  Dim RX As Object
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = True
  RX.Pattern = "[^" & Join(Application.Transpose(Range("D1", Range("D" & Rows.Count).End(xlUp))), "") & "]"
  For i = 1 To UBound(a)
    If Not RX.test(a(i, 1)) Then
      k = k + 1
      b(k, 1) = a(i, 1)
    End If
  Next i
  Range("B2").Resize(k).Value = b
End Sub

Results:

oaj1977.xlsm
ABCD
1WordsAllowedLetters:g
2aahsadadi
3aalsadaie
4aaniaddal
5aaruagada
6abacagagd
7abayagal
8abasaged
9abbaagee
10abbeagla
11abbyaide
12abbraiel
13abed
14abey
15abel
16abet
17abib
18abie
19abye
20abir
Sheet2
 
Upvote 0
Grrr - I'm getting a 429 Run-time error - ActiveX component can't create object. Debug is pointing to the line: Set RX=...
 
Upvote 0
I believe that is because you are using a Mac... I don't think the RegExp script language is not available for Macs. Did any of the other suggestions work for you?
 
Upvote 0
Yes, sorry, I had read that you were using a Mac but forgot when deciding to use RegExp. :oops:
Here is an alternative to produce the list as in column B of my post 13 screen shot.

VBA Code:
Sub Get_Allowed_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim sLike As String
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
 
  s = "*[!" & Join(Application.Transpose(Range("D1", Range("D" & Rows.Count).End(xlUp))), "") & "]*"
  For i = 1 To UBound(a)
    If Not a(i, 1) Like sLike Then
      k = k + 1
      b(k, 1) = a(i, 1)
    End If
  Next i
  Range("B2").Resize(k).Value = b
End Sub
 
Upvote 0
Yes, sorry, I had read that you were using a Mac but forgot when deciding to use RegExp. :oops:
Here is an alternative to produce the list as in column B of my post 13 screen shot.

VBA Code:
Sub Get_Allowed_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim sLike As String
 
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)

  s = "*[!" & Join(Application.Transpose(Range("D1", Range("D" & Rows.Count).End(xlUp))), "") & "]*"
  For i = 1 To UBound(a)
    If Not a(i, 1) Like sLike Then
      k = k + 1
      b(k, 1) = a(i, 1)
    End If
  Next i
  Range("B2").Resize(k).Value = b
End Sub
It runs, but it seems to be returning all words that contain any of the letters. It should only return words that contain ONLY those letters and not any others. The logic in the If/Then script seems to count words that contain any of the letters.
 
Upvote 0
The question I asked in Message #15 is still pending... "Did any of the other suggestions work for you?"
 
Upvote 0
Yes - I got one of the other suggestions to work -- thanks, everyone!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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