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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

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,101,771
Messages
5,482,822
Members
407,365
Latest member
Leah Ashley

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top