Cellwise Try it free

Excel guide · 4 min read

How to find the nth occurrence of a character in Excel

Quick answerFIND only returns the first match, so swap the nth one for a marker first: =FIND("#",SUBSTITUTE(A2,"-","#",2)) returns the position of the second hyphen in A2. SUBSTITUTE's fourth argument replaces only that instance. In Excel 365, =TEXTAFTER(A2,"-",2) returns the text after the second hyphen directly.

Sample data

Every result on this page was calculated from this sheet by the same spreadsheet engine the Cellwise app uses.

ABCD
1SKUFile pathInstance
2TX-204-BLK-LClients/Halden/2026/Q3/invoice-0912.pdf2
3TX-204-NVY-XLClients/Marsh & Co/2026/brief.docx
4HD-88-GRY-MJobs/Job #118 Pell St/photos/site-01.jpg
5CAP-12-REDClients/Ostrava/2025/Q4/Final/contract-signed.pdf
6BAG-3readme.txt
7tx-510-blk-sShared/templates/budget-v4.xlsx

Download this sheet as an Excel practice file (.xlsx)

Position of the 2nd hyphen with FIND and SUBSTITUTE

Column A holds product SKUs built from parts: style, model, color, size. FIND("-",A2) would give 3, the first hyphen. To reach the second, SUBSTITUTE(A2,"-","#",2) turns only the second hyphen into a #, so TX-204-BLK-L becomes TX-204#BLK-L. FIND then looks for the #.

Put the instance number in a cell (D2 here) instead of typing 2, and you can change which hyphen you want without editing formulas. This works in every version of Excel.

=FIND("#",SUBSTITUTE(A2,"-","#",D2))

Result: 7

7: the second hyphen in TX-204-BLK-L is the 7th character.

Text before and between occurrences

Once you have a position, LEFT and MID do the cutting. Everything before the second hyphen is LEFT(A2, position - 1), which gives TX-204, the style and model.

Text between the 2nd and 3rd hyphens needs both positions: start one character after the 2nd and take (3rd - 2nd - 1) characters. It is long, but each piece is the same pattern.

=MID(A2,FIND("#",SUBSTITUTE(A2,"-","#",2))+1,FIND("#",SUBSTITUTE(A2,"-","#",3))-FIND("#",SUBSTITUTE(A2,"-","#",2))-1)

Result: BLK

BLK, the color code. Fill it down and A3 gives NVY.

Excel 365: TEXTAFTER and TEXTBEFORE with an instance number

In Excel for Microsoft 365 and Excel 2024, TEXTAFTER(text, delimiter, instance_num) and TEXTBEFORE do all of this without positions. =TEXTBEFORE(A2,"-",2) returns TX-204. Nest them to get the middle piece: =TEXTBEFORE(TEXTAFTER(A2,"-",2),"-") returns BLK.

If the instance does not exist, both return #N/A. Their optional sixth argument, if_not_found, lets you return something else instead, or wrap them in IFERROR. If you have them, prefer these: they are far easier to read and to fix later.

=TEXTAFTER(A2,"-",2)

Result: BLK-L

BLK-L, everything after the second hyphen.

Text after the last slash (file name from a path)

For the last occurrence, count how many there are and use that count as the instance number. LEN(B2)-LEN(SUBSTITUTE(B2,"/","")) counts the slashes: the length drops by one for each slash removed. Here B2 has 4, so the 4th slash is the last.

In Excel 365 a negative instance number counts from the end, so =TEXTAFTER(B2,"/",-1) returns invoice-0912.pdf, and =TEXTBEFORE(B2,"/",-1) returns the folder, Clients/Halden/2026/Q3. That is the method to use when paths have a different number of folders on each row, like B3 and B5.

=MID(B2,FIND("|",SUBSTITUTE(B2,"/","|",LEN(B2)-LEN(SUBSTITUTE(B2,"/",""))))+1,200)

Result: invoice-0912.pdf

invoice-0912.pdf. On B5, which is two folders deeper, the same formula returns contract-signed.pdf. The 200 just means "the rest of the text".

Pick a marker that cannot appear in the text

The marker must never already be in the cell, or FIND stops at the wrong one. B4 is Jobs/Job #118 Pell St/photos/site-01.jpg. Swap the last slash for # and FIND finds the # in "Job #118" first, so the formula returns 118 Pell St/photos/site-01.jpg.

Use a character that your data never contains. "|" is used above; CHAR(1), a non-printing character, is the safest choice in long-lived workbooks.

=MID(B4,FIND("#",SUBSTITUTE(B4,"/","#",3))+1,200)

Result: 118 Pell St/photos/site-01.jpg

118 Pell St/photos/site-01.jpg, the wrong answer. With "|" as the marker it correctly returns site-01.jpg.

When there are fewer occurrences than you asked for

CAP-12-RED has only two hyphens and BAG-3 only one. If SUBSTITUTE finds no nth instance it changes nothing, FIND finds no marker, and the result is #VALUE!. Decide what should happen: a blank, the whole text, or a message. IFERROR handles it, and the counting trick tells the reader why.

=IFERROR(FIND("#",SUBSTITUTE(A6,"-","#",2)),"Only "&(LEN(A6)-LEN(SUBSTITUTE(A6,"-","")))&" hyphen")

Result: Only 1 hyphen

Only 1 hyphen, instead of #VALUE! for BAG-3.

Common mistakes

Searching for a letter in the wrong case

SUBSTITUTE and FIND are both case-sensitive, so a search for "BLK" misses tx-510-blk-s. Wrap the text in UPPER() first if case varies, or use SEARCH, which ignores case.

Using a marker that is already in the text

Pick a character that never appears in your data, such as "|" or CHAR(1), or switch to TEXTAFTER, which needs no marker.

Hard-coding the last instance number

Paths and codes rarely have the same number of parts on every row. Count them with LEN-LEN(SUBSTITUTE()) or use TEXTAFTER with -1.

Leaving #VALUE! in a report

Rows with fewer occurrences are normal. Wrap the formula in IFERROR and return a blank or the original text.

FAQ

How do I find the second occurrence of a character in Excel?

Use =FIND("#",SUBSTITUTE(A2,"-","#",2)), replacing "-" with your character. SUBSTITUTE swaps only the second one for a marker and FIND returns its position.

How do I get the text after the last slash in Excel?

In Excel 365, use =TEXTAFTER(A2,"/",-1). In older versions, use =MID(A2,FIND("|",SUBSTITUTE(A2,"/","|",LEN(A2)-LEN(SUBSTITUTE(A2,"/",""))))+1,200), which finds the last slash by counting them first.

How do I count how many times a character appears in a cell?

=LEN(A2)-LEN(SUBSTITUTE(A2,"-","")) counts the hyphens in A2. It is case-sensitive for letters; wrap both A2 references in UPPER() to count regardless of case.

How do I find the nth occurrence of a value in a column?

That is a lookup rather than a text task. In Excel 365, =INDEX(FILTER(B2:B20,A2:A20="Halden"),2) returns the second matching row's value from column B.

Functions used

Related guides

Updated