How to Grab All Text After The Last Occurence Using Excel

In a Microsoft Excel spreadsheet cell one can use a custom function to effectively grab all test after the final (last) occurrence of a particular character or string. This is accomplished by simply creating a Custom Function and then using that function in a cell formula. Following is the code and instructions for how to create and then implement the function.


Function PullAfterLast(rCell As Range, strLast As String)
PullAfterLast = Mid(rCell, InStrRev(rCell, strLast) + 1, 256)
End Function

  1. Highlight the above code, and copy it to the clipboard by pressing CTRL-C, or by right-clicking on the text and on the resulting popup window left-clicking on Copy.
  2. Open Microsoft Excel (or proceed to step 2 if you prefer to open Excel by clicking  the desired existing .xls file directly.).
  3. Open the workbook you wish to use the function with, or create a new workbook.
  4. Open the Visual Basic Editor window by pressing ALT-F11 on the keyboard, or clicking Tools > Macros > Visual Basic Editor
  5. Insert a new module by right-clicking on ThisWorkbook > Insert > Module or on the toolbar clicking Insert > Module
  6. Paste the clipboard contents (which is the code above this numbered procedure) into the code window by pressing CTRL-V, or by right-clicking in the code window and on the popup menu clicking on Paste, or by clicking the menu toolbar on Edit and clicking on Paste.
  7. Close the Visual Basic Editor by presing CTRL-Q, or by clicking File on the menu toolbar and then clicking Close and Return to Microsoft Excel.

The custom user defined function is now available to be used in the formulas of cells. To use it, the following syntax format is used in the cell’s formula:

=PullAfterLast(A1,"yourstring")

Of course, replace yourstring with the appropriate character or string.

This info was combined from ozgrid.com article about how to make custom functions easily, and vertex42.com article where Dave Hawley gave the universe this function’s code, many thanks!

Posted under Excel, Microsoft, Office