Extract 2nd to last word

mozjesse

New Member
Joined
Jul 18, 2008
Messages
4
How do I extract the second to last word from a string of text in one cell and put it another.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Do the strings of text have a consistency to them as far as how many spaces are in it?
 
Upvote 0
It's no pretty, and someone may be around with a shorter answers, but...

If your text is in B1

Enter the following in C1

*** Make sure that after pasting the formula below into the formula bar, you press, Control, Shift, and Enter at the same time.

=MID(B1,LARGE((MID(B1,ROW(A$1:INDEX(A:A,LEN(B1))),1)=" ")*ROW(A$1:INDEX(A:A,LEN(B1))),2)+1,LEN(B1)-1-MAX((MID(B1,ROW(A$1:INDEX(A:A,LEN(B1))),1)=" ")*ROW(A$1:INDEX(A:A,LEN(B1)))))
 
Upvote 0
That didn't work :-(

The data looks like this: 01019000 - Los demás 10 A

What I want is to get the "10" but the number may be one to three digits and the description may be short or quite long... either way the string always ends in a number and then a letter... I want the number.

Thanks
 
Upvote 0
A UDF method:
Code:
'=RField(A1, 2)
Function RField(cell, pos As Integer, Optional delimit As String = " ")
  Dim a() As String
  a() = Split(cell, delimit)
  RField = a(UBound(a) - pos + 1)
End Function
 
Upvote 0
mozjesse,

I like Barry's solution (in the below screenshot, the entries in bold).


But, another way:

Excel Workbook
ABC
1This is a test to find the second to last word in a very long string.longlong
2This is a test to find the second to last word in a long string.longlong
3This is a test to find the second to last word in a string.aa
4This is a test to find the second to lasttoto
501019000 - Los dems 10 A1010
Sheet1



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Option Compare Text
Function Get_Word(text_string As String, nth_word) As String
'
' http://www.ozgrid.com/News/jul-2005.htm
'
Dim lWordCount As Long
    With Application.WorksheetFunction
        lWordCount = Len(text_string) - Len(.Substitute(text_string, " ", "")) + 1
        If IsNumeric(nth_word) Then
           nth_word = nth_word - 1
            Get_Word = Mid(Mid(Mid(.Substitute(text_string, " ", "^", nth_word), 1, 256), _
                .Find("^", .Substitute(text_string, " ", "^", nth_word)), 256), 2, _
                .Find(" ", Mid(Mid(.Substitute(text_string, " ", "^", nth_word), 1, 256), _
                .Find("^", .Substitute(text_string, " ", "^", nth_word)), 256)) - 2)
        ElseIf nth_word = "First" Then
            Get_Word = Left(text_string, .Find(" ", text_string) - 1)
        ElseIf nth_word = "Last" Then
            Get_Word = Mid(.Substitute(text_string, " ", "^", Len(text_string) - _
            Len(.Substitute(text_string, " ", ""))), .Find("^", .Substitute(text_string, " ", "^", _
            Len(text_string) - Len(.Substitute(text_string, " ", "")))) + 1, 256)
        End If
    End With
End Function


To use the "Get_Word" Function, I counted the number of spaces in the string with:
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))

The formula/function in cell B1 (copied down) (to get the second to last word):
=Get_Word(A1,LEN(A1)-LEN(SUBSTITUTE(A1," ","")))


Have a great day,
Stan
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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