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?
 
It runs, but it seems to be returning all words that contain any of the letters.
Yes, the code was incorrect. I edited the code as I posted and missed one correction. Oops again. :oops:
If you wish, you could give this corrected version a try. Also modified to be not case-sensitive.

VBA Code:
Sub Get_Allowed_v3()
  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)
 
  sLike = "*[!" & UCase(Join(Application.Transpose(Range("D1", Range("D" & Rows.Count).End(xlUp))), "")) & "]*"
  For i = 1 To UBound(a)
    If Not UCase(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

Some sample data and results:

oaj1977.xlsm
ABCD
1WordsAllowedLetters:d
2Catdogo
3doggodt
4mouseGodotg
5godtoogood
6Godot
7togs
8ban
9toogood
10
Sheet3
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,330
Latest member
ThatGuyCap

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