help converting complicated formula into VBA

tboetticher

New Member
Joined
Feb 24, 2014
Messages
4
If anyone can help me, I would appreciate the help. I am looking at creating a new function in excel. The function would perform the below action.

function Extract_text(Lookup_Value, Within_Text, Number of Characters)

Substitute(if(iserror(search("Lookup_value","within_text")),"",mid("Within_Text",search("Lookup_value", "Within_text")),number of Characters)),"","")
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Can I ask why you don't want to use the function? Built in functions are faster than VBA.
 
Upvote 0
Hey Brian,

Thanks for responding. I am looking to convert this formula into a function in order to speed up my business process and decrease the amount of time I spend re writing the formula or modifying the formula. I am also going to use the customer function to help me build several other functions that are much longer and more time intensive.
 
Upvote 0
Ok. Instead of the formula can you give us an idea of what you will input and what you'd like to be returned?
 
Upvote 0
I use this formula to retrieve information from within large strings in a near by cell. It uses search to find the particular text I'm looking for ie "Milk" and returns a predetermined number of characters.
 
Upvote 0
You can shorten that formula.

=IF(ISNUMBER(SEARCH("lookup","within_text")),MID("within_text",SEARCH("lookup","within_text"),Num of Chars),"")

If you really want to do it through VBA then here is the function.

Code:
Public Function ExtractText(Lookup_Value As String, Within_Text As String, Chars As Integer)
    Dim s As String
    If Not UCase(Within_Text) Like "*" & UCase(Lookup_Value) & "*" Then
        ExtractText = ""
        Exit Function
    Else
        ExtractText = Mid(Within_Text, InStr(Within_Text, Lookup_Value), Chars)
    End If


End Function
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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