Extracting part of a string

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Can't quite decide on a best method to achieve this.
I have a string delimited by vblf characters
One of the rows may start with "All" then " * by *"
If true, I want to get everything after "by " to end of that row (it will never be the last row in the string).
Not sure if RegEx or Filter will do it, or just split on vblf and check each row. Or something else?

Any suggestions ?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Like this?

=REPLACE(A1,1,SEARCH("by",A1,SEARCH("All",A1)+3)+3,"")
 
Upvote 0
Looks good and concise but will that work in VBA or just as a worksheet function?
This is in a module and the string is not in a sheet,
 
Upvote 0
One way:
VBA Code:
Public Function ExtractingPart(s As String) As String
Dim v As Variant
For Each v In Split(s, vbLf)
If v Like "All*by*" Then
    ExtractingPart = VBA.Mid(v, InStr(1, v, "by") + 3)
    Exit For
End If
Next
End Function
 
Upvote 0
Solution
If the above does not resolve your question, please give us a smallish but representative set of sample data AND the expected results with XL2BB so that we can be sure of your data structure and expected results and can test with that sample data.
 
Upvote 0
Hey, I like it! Much better than I had... For each is brilliant as there is a varying number of rows
Many thanks.
 
Upvote 0
Perhaps your data could never have anything like this but note the incorrect result from that previous UDF in C1 below.
It could be corrected as follows (& used in column D below).

VBA Code:
Public Function ExtractPart(s As String) As String
Dim v As Variant
For Each v In Split(s, vbLf)
If v Like "All* by *" Then
    ExtractPart = VBA.Mid(v, InStr(1, v, " by ") + 4)
    Exit For
End If
Next
End Function

But just as an option, and since you mentioned RegEx, here is a non-looping alternative function as used in column B below.
BTW, does "All" at the beginning on the line need to have a space after it? You didn't indicate that here but I'm wondering ..
One of the rows may start with "All" then " * by *"

VBA Code:
Function GetPart(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = Replace("(^|#)(All.* by )(.+?)(?=#)", "#", vbLf)
    If .Test(s) Then GetPart = .Execute(s)(0).SubMatches(2)
  End With
End Function

AlexanderBB.xlsm
ABCD
1One or two words Allow that abysses are dangerous All other by this time are gonethis timeses are dangerousthis time
2one or two words Any other by this time are gone   
3All other by this time are gonethis timethis timethis time
Sheet1
Cell Formulas
RangeFormula
B1:B3B1=GetPart(A1)
C1:C3C1=ExtractingPart(A1)
D1:D3D1=ExtractPart(A1)
 
Last edited:
Upvote 0
That's even better solution Peter, as no looping or split needed (and I do need to test 2 strings)
Amazing what RegEx or do. And that anyone understands it all ! Yes there'll always be a space after the "All".
Thanks very much
 
Upvote 0
Yes there'll always be a space after the "All".
Then try this version
Rich (BB code):
Function GetPart(s As String) As String
  With CreateObject("VBScript.RegExp")
    .Pattern = Replace("(^|#)(All .* by  )(.+?)(?=#)", "#", vbLf)
    If .Test(Replace(s, " ", "  ")) Then GetPart = Mid(.Execute(Replace(s, " ", "  "))(0).SubMatches(2), 2)
  End With
End Function
Note that there are two space characters where highlighted
1694323202981.png


AlexanderBB.xlsm
AB
1One or two words Allow that and by the way All by yourself Goyourself
2One or two words All other by this time Allow that and by the way Gothis time
3All by yourself go by trainyourself
4All other by this time are gonethis time
Sheet1
Cell Formulas
RangeFormula
B1:B4B1=GetPart(A1)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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