Check for letters in words

YasserKhalil
PlatinumLounger
Posts: 4967
Joined: 31 Aug 2016, 09:02

Check for letters in words

Post by YasserKhalil »

Hello everyone

I have a textbox that the user will input some random and scattered letters, for example: lacol
and in column A there are words that I need to check the letters based on the input of textbox

coal
cool
local
call


these are four words as an example. how can I check for the existence of the letters of the inputted textbox corresponding to each word?
If I use UDF for example to check "coal", the udf should return True as all letters of "coal" included
as for the word "cool" the udf should reutrn False as it has the letters c, o, l but the extra letter o doesn't exist anymore

so the results should be >>
coal = True
cool = False
local = True
call = True

User avatar
rory
5StarLounger
Posts: 826
Joined: 24 Jan 2010, 15:56

Re: Check for letters in words

Post by rory »

You could do something like this:

Code: Select all

Function TextCheck(ByVal inputText As String, ByVal checkText As String) As Boolean
   Dim n As Long
   TextCheck = True
   If Len(checkText) > Len(inputText) Then
      TextCheck = False
   Else
      For n = 1 To Len(checkText)
         Dim findPos As Long
         findPos = InStr(1, inputText, Mid$(checkText, n, 1), vbTextCompare)
         If findPos <> 0 Then
            Mid(inputText, findPos, 1) = Chr(0)
         Else
            TextCheck = False
            Exit For
         End If
      Next n
   End If
End Function
Regards,
Rory

User avatar
StuartR
Administrator
Posts: 12856
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: Check for letters in words

Post by StuartR »

Clever
StuartR


YasserKhalil
PlatinumLounger
Posts: 4967
Joined: 31 Aug 2016, 09:02

Re: Check for letters in words

Post by YasserKhalil »

Amazing. Thank you very much Mr. Rory for this perfect function.