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.

스크린샷_2019-07-13_오후_9.43.30.png



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.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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.
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,025
Members
448,543
Latest member
MartinLarkin

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