vba function to extract between two delimiters

johnmerlino

Board Regular
Joined
Sep 21, 2010
Messages
94
Hey all,

This function:

Mid( text, start_position, number_of_characters )


won't help if I want to extract between two delimiters like an & and &W:

abc def & abc xyz &W abc

For example:
pos = InStr(Range("A" & i), " &W ")
pos2 = InStr(Range("A" & i), " & ")
main_name = Mid(Range("A" & i).Value, pos2 + 3, pos)

will pull:

abc xyz &W abc

not:

abc xyz

because pos is not treated as end delimiter but rather the length of characters to pull, which in this case will be 18.

So I wonder what function would be suitable to extract:

abc xyz


Thanks for response
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try something like this
Code:
Function Between_Ands(ByVal Full_String As String, ByVal First_Delimiter As String, ByVal Second_Delimiter As String) As String
Pos = InStr(Full_String, First_Delimiter)
pos2 = InStr(Full_String, Second_Delimiter)
If Pos = 0 Or pos2 = 0 Then
    Between_Ands = "Missing Delimiter"
    Exit Function
End If
Between_Ands = Mid(Full_String, Pos + Len(First_Delimiter), pos2 - (Pos + Len(First_Delimiter)))
End Function
 
Upvote 0
thanks for response

This line right here gives me invalid procedure call:

Code:
Mid(Range("A" & i).Value, pos + Len("&"), pos2 - (pos + Len("&")))

for this cell:

abc def & abc xyz &W abc
 
Upvote 0
What was wrong with the code I gave you?

Maybe you didn't understand how to use the formula. Your input (on the worksheet) should look like
Code:
 =Between_Ands(A1,"&","&W")
 
Upvote 0
Hi

Another option:

Code:
main_name = Split(Split(Range("A" & i).Value, " &W ")(0), " & ")(1)
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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