Convert Excel formula to UDF function

shafiey

Board Regular
Joined
Sep 6, 2023
Messages
60
Office Version
  1. 365
Platform
  1. Windows
Hi, Is it possible to write the formula in cell D2 as a UDF Function?
The file is in Persian language.
I want the formula to search which of the words in column B of the data sheet is in the text of cell C2 of the keywords sheet and return it if it exists.
Many thanks.

Shafiey20230910
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
The UDF below works with English words. I can only guess but it should work if your workbook language is Persian.

Option Explicit

VBA Code:
Function StringsInString(psSearchIn, psSearchFor)

'   Array holding words in string to search for.
    Dim sSearchFor() As String
    
'   Array holding words in string to search within.
    Dim sSearchIn() As String
    
'   Used to iterate through words in string to search for.
    Dim iSearchForWords As Long

'   Used to iterate through words in string to search within.
    Dim iSearchInWords As Long

'   Fill array with words in string to search for.
    sSearchFor = Split(" " & psSearchFor, " ")
    
'   Fill array with words in string to search within.
    sSearchIn = Split(" " & psSearchIn, " ")
     
'   Iterate through words to search for.
    For iSearchForWords = 1 To UBound(sSearchFor)
    
'       Iterate through words in string to search within.
        For iSearchInWords = 1 To UBound(sSearchIn)
            
'           Check if word to search for is in string to search within.
            If sSearchFor(iSearchForWords) = sSearchIn(iSearchInWords) _
             Then
                
'               Add space between words found.
                If StringsInString <> "" Then StringsInString = StringsInString & " "
                
'               Add string found to return value.
                StringsInString = StringsInString & sSearchFor(iSearchForWords)
            End If
            
        Next iSearchInWords
    
    Next iSearchForWords
    
'   Remove extra space at the end of the return string, if any.
    StringsInString = Trim(StringsInString)

End Function
 
Upvote 0
The UDF below works with English words. I can only guess but it should work if your workbook language is Persian.

Option Explicit

VBA Code:
Function StringsInString(psSearchIn, psSearchFor)

'   Array holding words in string to search for.
    Dim sSearchFor() As String
   
'   Array holding words in string to search within.
    Dim sSearchIn() As String
   
'   Used to iterate through words in string to search for.
    Dim iSearchForWords As Long

'   Used to iterate through words in string to search within.
    Dim iSearchInWords As Long

'   Fill array with words in string to search for.
    sSearchFor = Split(" " & psSearchFor, " ")
   
'   Fill array with words in string to search within.
    sSearchIn = Split(" " & psSearchIn, " ")
    
'   Iterate through words to search for.
    For iSearchForWords = 1 To UBound(sSearchFor)
   
'       Iterate through words in string to search within.
        For iSearchInWords = 1 To UBound(sSearchIn)
           
'           Check if word to search for is in string to search within.
            If sSearchFor(iSearchForWords) = sSearchIn(iSearchInWords) _
             Then
               
'               Add space between words found.
                If StringsInString <> "" Then StringsInString = StringsInString & " "
               
'               Add string found to return value.
                StringsInString = StringsInString & sSearchFor(iSearchForWords)
            End If
           
        Next iSearchInWords
   
    Next iSearchForWords
   
'   Remove extra space at the end of the return string, if any.
    StringsInString = Trim(StringsInString)

End Function
Thank you for your attention.
What formula should I write in cell D2?
 
Upvote 0
THIS WORKBOOK shows how to use the UDF that I provided.
Thank you.
I want it for Persian language. In the Persian language, some words (for example: ی, ها, های and etc.) are suffixes of some other words that are suffixed to them completely or with a half space. This should also be considered. For example, in the words آبها or آب‌ها, the word آب must be derived from both. In my opinion, a column for suffixes should be considered, so that if they are connected to a word, that word is also extracted, and it requires coding, which I do not know.
Sincerely
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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