Extract all the words (comma seperated) that start with specific combination of alphabets and numbers from a string

sarad agarwal

New Member
Joined
Mar 14, 2016
Messages
20
Hi All,

I want to Extract all the words (comma separated) that start with specific combination of alphabets and numbers from a string.

For example: if my string is as follows:

1) "My name is Sarad and my phone number is B023456 and the number of my car is B09876."

Now I want all the words that start with B0 in the string above, so my output should be B023456,B09876.

Any help is much appreciated.

<tbody>
</tbody>
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If you have the TEXTJOIN function (new in some versions of Excel 2016 or 365), you can use the formula in A3. If not, you'll have to use some variant of VBA, such as the UDF in A5:

A
1My name is Sarad and my phone number is B023456 and the number of my car is B09876.
2
3B023456,B09876.
4
5B023456,B09876.

<colgroup><col style="width: 25pxpx"><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet6

Worksheet Formulas
CellFormula
A5=getb0(A1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Array Formulas
CellFormula
A3{=TEXTJOIN(",",TRUE,IF(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-1)),2)="B0",TRIM(LEFT(SUBSTITUTE(MID(A1,ROW(INDIRECT("1:"&LEN(A1)-1)),LEN(A1))," ",REPT(" ",LEN(A1))),LEN(A1))),""))}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Code:
Public Function GetB0(ByVal target As Range)
Dim w As Variant, x As Variant

    w = Split(target, " ")
    For Each x In w
        If Left(x, 2) = "B0" Then GetB0 = GetB0 & x & ","
    Next x
    If Len(GetB0) > 0 Then GetB0 = Left(GetB0, Len(GetB0) - 1)
End Function
 
Upvote 0
I would create my own function in VBA to do it, like this:
Code:
Function MySplit(myCell As Range)

    Dim txtString As String
    Dim counter As Integer
    Dim wordVar() As String
    Dim resString As String

    txtString = myCell.Value
    wordVar = Split(txtString, " ")
    For counter = 0 To UBound(wordVar)
        If Left(wordVar(counter), 2) = "B0" Then
           resString = resString & wordVar(counter) & ","
        End If
    Next counter

    If Len(resString) > 0 Then MySplit = Left(resString, Len(resString) - 1)

End Function
Then, you can use it like any other Excel function.
So, if your sentence was in cell H1, just use this formula:
Code:
=mysplit(H1)
 
Upvote 0

Forum statistics

Threads
1,216,089
Messages
6,128,760
Members
449,466
Latest member
Peter Juhnke

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