• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Find the Last Occurrence of a String In Another String

You are here: Home / Excel Formulas / Find the Last Occurrence of a String In Another String
Find last occurrence of a string in another string
July 24, 2019 by Philip Treacy

If we want to find whether or not a character or string occurs within another string we can use FIND or SEARCH. But these functions only tell us if a string (or character) exist in another string, they don't tell us if the string we are looking for occurs multiple times.

For example, we have a path\filename like so:

c:\excel\reports\march.xlsx

and we want to extract the filename. To do this we have to find the position of the last \ and extract everything to the right.

FIND and SEARCH will both give the result 3, the position of the first \, so we have to use a trick involving SUBSTITUTE to find the last one.

If we know the length of the string,

LEN("c:\excel\reports\march.xlsx") = 27

and we know the length of the string after all the \ are removed,

LEN("c:excelreportsmarch.xlsx") = 24

then we know how many \ are in the string, in this case 27 - 24 = 3.

To remove all of the \ use SUBSTITUTE to replace \ with an empty string "" (our string is in A1)

SUBSTITUTE(A1,"\","")

So

LEN(SUBSTITUTE(A1,"\","")) = 24

We can call SUBSTITUTE using it's fourth argument to tell it to replace the last \ with another character which we will use later. I'm using CHAR(9) which is a character I know won't appear in the path to my file.

SUBSTITUTE(A1,"\",CHAR(9),LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))

Where A1 contains our string and

LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))

tells SUBSTITUTE to replace the 3rd \

I can now use SEARCH or FIND to give me the position of my CHAR(9) character.

SEARCH(CHAR(9),SUBSTITUTE(A1,"\",CHAR(9),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))

Knowing this I can use RIGHT to extract the filename. We can work out the number of characters for RIGHT to extract by subtracting the position of CHAR(9) from the length of the string.

RIGHT(A1,LEN(A1)-SEARCH(CHAR(9),SUBSTITUTE(A1,"\",CHAR(9),LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))

If we want the path rather than the filename, we can use LEFT.

LEFT(A1,SEARCH(CHAR(9),SUBSTITUTE(A1,"\",CHAR(9),LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))))

Flash Fill

You can achieve the same results using Flash Fill, but Flash Fill does not automatically update results.

If your source data changes, you'll need to re-run Flash Fill, whereas a formula will automatically update.

InstrRev - VBA Function

If you want to use VBA, you can achieve everything in this post using InstrRev which is one of a number of useful VBA string functions.

Download Example Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.
Please enter a valid email address.

Download the Excel Workbook. Note: This is a .xlsx file please ensure your browser doesn't change the file extension on download.

Find last occurrence of a string in another string
Philip Treacy

Microsoft Power BI Community Super User Logo

AUTHOR Philip Treacy Co-Founder / Owner at My Online Training Hub

Systems Engineer with 30+ years working for companies like Credit Suisse and E.D.S. in roles as varied as Network & Server Support, Team Leader and Consultant Project Manager.

These days Philip does a lot of programming in VBA for Excel, as well as PHP, JavaScript and HTML/CSS for web development.

He's particularly keen on Power Query where he writes a lot of M code.

When not writing blog posts or programming for My Online Training Hub, Philip can be found answering questions on the Microsoft Power BI Community forums where he is a Super User.

More Excel Formulas Posts

Summarize Months to Quarters

Excel Formulas to Summarise Monthly Data into Quarters

3 ways (good, better, best) to summarize monthly data into quarters using formulas. Lots of examples and sample file to download.
Excel BYROW and BYCOL Functions

Excel BYCOL and BYROW Functions

Excel BYCOL and BYROW functions fundamentally change the way we write formulas that calculate across columns and down rows.
python in excel natively

How to Use Python in Excel Natively

How to use Python in Excel natively using libraries like Pandas, NumPy, Matplotlib, Seaborn and more for analysis and spectacular charts!
excel dynamic named ranges

Excel Dynamic Named Ranges

Excel Dynamic Named Ranges update automatically to include new data in the ranges referenced in your formulas and PivotTables etc.
functions for financial modelling

Excel Functions for Financial Modeling

Top 23 must know Excel functions for Financial Modeling. Includes example Excel file and step by step instructions.
excel formula by example

Excel Formula by Example

Excel can now write a formula by example. Simply give it an example or two of the result and Excel will write the formula.
ai-aided excel formula editor

AI Aided Excel Formula Editor

Save time with this free AI Excel formula editor add-in that writes, edits, improves and interprets formulas for you!
top excel functions for data analysts

Top Excel Functions for Data Analysts

Must know Excel Functions for Data Analysts and what functions you don’t have to waste time learning and why.
excel advanced formula environment

Excel Labs (Formerly, Advanced Formula Environment)

Excel Labs is a long awaited, new improved way to write, name and store Excel formulas, including LAMBDAS with the help of AI.
Pro Excel Formula Writing Tips

Pro Excel Formula Writing Tips

Must know Excel formula writing tips, tricks and tools to make you an Excel formula ninja, including a new formula editor.


Category: Excel Formulas
Previous Post:Excel PivotTable Field List Tips
Next Post:Working With Comments in VBAworking with comments in vba

Reader Interactions

Comments

  1. sam

    August 1, 2021 at 6:53 pm

    Is it possible to solve this with LAMBA ?

    Reply
    • Mynda Treacy

      August 5, 2021 at 11:23 am

      Yes, anything you can do with an existing function can be done with a LAMBDA, but there’s not much point in writing your own function when there’s an existing solution available with built in functions.

      Reply
  2. David N

    August 1, 2019 at 5:50 am

    I personally prefer to use REPLACE instead of RIGHT to isolate the end of a string because it avoids the need for any “length minus find” math games. And the formula turns out to be one character shorter in this case, so it’s a virtual break-even on typing.

    =REPLACE(A1,1,SEARCH(CHAR(9), SUBSTITUTE(A1,”\”,CHAR(9),LEN(A1)- LEN(SUBSTITUTE(A1,”\”,””)))),””)

    Reply
  3. Duncan Williamson

    July 25, 2019 at 9:15 am

    Alternatively, use Flash Fill …

    Reply
    • Philip Treacy

      July 25, 2019 at 9:34 am

      Thanks Duncan, yes that is true. Unfortunately FF does not automatically update when the source data changes. I’ve added a note to the post to make this clear.

      Thanks

      Phil

      Reply
  4. Diane Smith

    July 24, 2019 at 11:20 pm

    Too bad that Excel doesn’t have the function InStrRev that Microsoft Access has.

    Then the formulas could be:

    FileName: Right(A1,Len(A1)-InStrRev(A1,”\”))

    PathName Left(A1,InStrRev(A1,”\”)-1)

    much simpler

    Reply
    • Philip Treacy

      July 25, 2019 at 8:40 am

      Hi Diane,

      Yes it would be nice to have this in the sheet, but you can use InStrRev with VBA.

      VBA String Functions

      Cheers

      Phil

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

Popular Content

  • 10 Common Excel Mistakes to Avoid
  • Top Excel Functions for Data Analysts
  • Secrets to Building Excel Dashboards in Less Than 15 Minutes
  • Pro Excel Formula Writing Tips
  • Hidden Excel Double-Click Shortcuts
  • Top 10 Intermediate Excel Functions
  • 5 Pro Excel Dashboard Design Tips
  • 5 Excel SUM Function Tricks
  • 239 Excel Keyboard Shortcuts

100 Excel Tips and Tricks eBook

Download Free Tips & Tricks

239 Excel Keyboard Shortcuts

Download Free PDF

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.