Extract the Last Word from a Cell in Excel - User Defined Delimiter Text Extraction - UDF

Add to Favorites

This UDF (user defined function) extracts the last word or characters from a cell in Excel. This is done by finding the last separator (i.e. a space) before a set of words or characters in Excel and then returning what comes after that last separator. This means that you can return characters, part numbers, etc. that aren't whole words as long as there is a space, dash, comma, period, etc. before what you want to extract from the cell. The general explanation for this is that you can return the last word from a cell in Excel

This is a very easy function to use and has only two arguments, text and separator. The text argument is either actual text or a cell reference. The separator argument is an optional argument and refers to what separates the words. If you leave this argument blank, the default setting is a space.

Where to install the macro:  Module

UDF to Extract the Last Word from a Cell in Excel - User Defined Delimiter Text Extraction

Function GETLASTWORD(Text As String, Optional Separator As Variant)

Dim lastword As String

If IsMissing(Separator) Then
 Separator = " "
End If

lastword = StrReverse(Text)
lastword = Left(lastword, InStr(1, lastword, Separator, vbTextCompare))

GETLASTWORD = StrReverse(Replace(lastword, Separator, ""))

End Function








How to Install the Macro
  1. Select and copy the text from within the grey box above.

  2. Open the Microsoft Excel file in which you would like the Macro to function.

  3. Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.  Or For other ways to get there, Click Here.

  4. On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.

  5. If the Macro goes in a Module, Click Here, otherwise continue to Step 8.

  6. If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.

  7. If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.

  8. Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.

  9. You are now ready to run the macro.


Excel Forum