I'd like to change Excel Function to VBA Code.
Results 1 to 2 of 2

Thread: I'd like to change Excel Function to VBA Code.
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jul 2019
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    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.

  2. #2
    Board Regular
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    3,402
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: I'd like to change Excel Function to VBA Code.

    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.
    Short Guide to Better VBA - Link: https://www.mrexcel.com/forum/showthread.php?t=712119

    Please use code tags around your code:
    [Code] Your code here... [/Code]




    Engelse lessen, persoonlijk en doelgericht. Dutch tuition tailor-made for you. https://Ennef.nl

    Wearable for people with panic attacks: sidjup https://sidjup.com

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •