I'd like to change Excel Function to VBA Code.

Soomin

New Member
Joined
Jul 13, 2019
Messages
1
Hi Guys,

I would like to change excel function to VBA Code.
Here is the excel function,
=IF(AND(ISERROR(FIND(" ",E3))),IF(LEN(E3)<3,E3&"?"&$F$1&E3&"??",E3&"*"),IF(LEN(E3)>3,SUBSTITUTE(E3," ","")&"*"&$F$1&$E$1&E3&$E$1,SUBSTITUTE(E3," ","")&"?"&$F$1&SUBSTITUTE(E3," ","")&"??"&$F$1&$E$1&E3&$E$1))


1) Basically, if Len(product_name) is less than 3, it should be turned into ~? OR ~??.
UK -> UK? OR UK??

2) And, If there is space in product_name, the space should be deleted.
U K -> UK
United Kingdom -> UnitedKingdom

3) And, If there is space in product_product, it should start with ", and end with ".
U K -> "U K"
United Kingdom -> "United Kingdom"

4) And, if lend(product_name) is more than 2 or same with 3, it should end with *.
UntiedKingdom -> UnitedKingdom*​


5) Finally, all of these keywords should be merged in one cell, and connected with " OR "
United Kingdom -> UnitedKingdom* OR "United Kingdom"
U K -> UK? OR UK?? OR "U K"
UK -> UK? OR UK??
UnitedKingdom -> UnitedKingdom*




And the result is here.




But the problem is that the size of this excel file is too big so it's not easy to deal with.

So I am gonna try VBA so I can easily handle this file.





I can search and learn the basic VBA code.
I just need the SUBSTITUTE(cell," ","") function and FIND(" ", cell) function, cause I have some problem in searching these two function in VBA code.

I know there is REPLACE code in VBA, but still it's so difficult for me T^T.

If you have the solution or if you know the website I can get the solution on, Please let me know.

Thanks a lot.
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
3,661
Code:
Function CheckWords(sCell As String) As String
    Dim sComp As String, sOrig As String
    Dim iL As Integer
    
    sOrig = Trim(sCell)
    iL = Len(sOrig)
    
    If iL Then
        If iL < 4 Then  '--------- Short words --------
            sComp = sOrig & "? OR " & sOrig & "??"
            If InStr(1, sOrig, " ") Then
                sComp = sComp & " OR """ & sOrig & """"
            End If
        Else            '--------- Long words ---------
            sComp = Replace(sOrig, " ", "") & "*"
            If InStr(1, sOrig, " ") Then
                sComp = sComp & " OR """ & sOrig & """"
            End If
        End If
        
        CheckWords = sComp
    End If
End Function
You can call this function from Excel like any standard function. If the word is in B2 and you want the result in G2 then in G2 enter =checkwords(b2)
You will notice that Excel will show the function in its list of proposed functions as well.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,478
Messages
5,487,124
Members
407,580
Latest member
nilnil1

This Week's Hot Topics

Top