VBA extract part of string using a specific word

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, Is it possible to extract part of a string using a specific word but return the length of the text.
There will always be numbers before the word and is not case sensitive.

Example:

The Text I want to extract is the length of "way"

Something 40way Nothing40way
Is it 12 way12 way
20WAY is found20WAY
This 18 Way is here18 Way

<tbody>
</tbody>
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this UDF:-

Code:
Function Way(txt [COLOR="Navy"]As[/COLOR] String) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 sp = Split(txt, " ")
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(sp)
    [COLOR="Navy"]If[/COLOR] InStr(UCase(sp(n)), "WAY") > 0 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Len(sp(n)) = 3 [COLOR="Navy"]Then[/COLOR]
            Way = sp(n - 1) & " " & sp(n)
        [COLOR="Navy"]Else[/COLOR]
            Way = sp(n)
        [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Exit[/COLOR] For
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] Function
Regards Mick
 
Upvote 0
Hi MickG, How would i incorporate this into a sub
 
Upvote 0
Perhaps like this for Data in column "A".
Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Dn.Offset(, 1).Value = Way(Dn.Value)
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Function Way(txt [COLOR="Navy"]As[/COLOR] String) [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
 sp = Split(txt, " ")
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(sp)
    [COLOR="Navy"]If[/COLOR] InStr(UCase(sp(n)), "WAY") > 0 [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Len(sp(n)) = 3 [COLOR="Navy"]Then[/COLOR]
            Way = sp(n - 1) & " " & sp(n)
        [COLOR="Navy"]Else[/COLOR]
            Way = sp(n)
        [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]Exit[/COLOR] For
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] Function
Regards Mick
 
Upvote 0
Thank you, this is what I need. Sorry for late reply
 
Upvote 0
Another way (excuse the pun)
Code:
Sub LenWay()
  Dim c As Range
  
  With CreateObject("VBSCript.RegExp")
    .IgnoreCase = True
    .Pattern = "\d+ *way"
    For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
      If .Test(c.Value) Then c.Offset(, 1).Value = .Execute(c.Value)(0)
    Next c
  End With
End Sub

Note that my code & Mick's may not produce the same results as mine specifically looks for digits before the "way" so if a cell contained "Anyway it is 12way", Mick's code will return "Anyway" whereas mine will return "12way"
 
Upvote 0
Hi Peter_SSs, I think this may be the best way forward for the end user, Thanks for your help
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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