Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding the Nth Occurrence of a Character.

# Finding the Nth Occurrence of a Character

by Allen Wyatt
(last updated September 19, 2018)

Barry often finds himself wanting to identify the Nth occurrence of a character within a text string. He knows he can use the SEARCH and FIND worksheet functions for finding an initial occurrence, but is unsure how to find, say, the 3rd occurrence of the letter "B" within a text string.

Actually, the SEARCH function could be used to find the desired occurrence, in the following manner:

```=SEARCHB("b",G20,(SEARCHB("b",G20,(SEARCHB("b",G20,1)+1))+1))
```

Notice how the SEARCHB function is used in a nested manner. The formula specifies what is being searched for (the letter "b") and the number of nesting levels indicates which occurrence within the cell you want to find. The formula returns the position of the desired character within the cell.

The problem with such a formula, of course, is that it is difficult to maintain and can quickly get unusable if you want to find, say, the seventh occurrence.

A more flexible formula would be the following:

```=FIND(CHAR(1),SUBSTITUTE(A1,"B",CHAR(1),3))
```

This formula examines the value in A1. It substitutes the CHAR(1) code for the third occurrence of "B" within the cell. The FIND function then looks within the resulting string for the position where CHAR(1) occurs. If the desired occurrence does not exist, then the formula returns a #VALUE error.

If you prefer, you could create a user-defined function that will look for the Nth position of a character. The following is a very simple macro that takes three arguments: the string to be searched, the text to match, and the position desired.

```Function FindN(sFindWhat As String, _
sInputString As String, N As Integer) As Integer
Dim J As Integer

FindN = 0
For J = 1 To N
FindN = InStr(FindN + 1, sInputString, sFindWhat)
If FindN = 0 Then Exit For
Next
End Function
```

The function is case sensitive in what it searches for, and it returns the position within the specified string at which the sFindWhat value occurs. If there is no occurrence at the specified instance, then the function returns a 0. The following shows how the function can be used in a worksheet:

```=FindN("b",C15,3)
```

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10567) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Finding the Nth Occurrence of a Character.

##### Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

##### MORE FROM ALLEN

Quickly Inserting the Date Your Way

Tired of messing with inserting the date and then changing it to a format that is more to your liking? There's a quick ...

Discover More

Selecting All Visible Worksheets in a Macro

Do you need your macro to select all the visible worksheets (and just the visible ones)? It's not as easy as it sounds, ...

Discover More

Using Graphics to Represent Data Series

You can spice up your bar chart by using a graphic, of your choosing, to construct the bars. This tip shows how easy it ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

##### More ExcelTips (ribbon)

Adding Area Codes to Phone Numbers

If you keep phone numbers in an Excel worksheet, you may need a way to add an area code to the beginning of all of the ...

Discover More

Determining If a Value is Out of Limits

Need to figure out if a value is outside of some arbitrary limit related to a different value? There are a number of ways ...

Discover More

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 7?

2019-10-07 12:30:40

Roy

@Ryn:

Lord son, you see the various things people are asking in this one as variants of the simple idea and you still ask that question?

It is because people go off on whatever tangents based on existing work. Even if that is their own start on a problem, not someone else's spreadsheet which their boss wants no substantive changes to the logic in. Everything in life pretty much does that which is why aliens did NOT have to come around and give us all our scientific advances. We can track most all of them for the work they came out of. And see why they did not happen earlier: they couldn't be used then, by and large, with what already existed. So the spreadsheet. Even when an idea could be done without by taking a different approach, one often is somehow wed to an approach that needs it. Or a variant.

An explicit answer to this one? I, for instance, would like to use the idea to get the "fourth to the last" and "third to the last" occurrence of a character by finding how many there are altogether, then subracting 3 and 2, and finally doing something like this looking for whatever number characters those two calculations came up with. Boss doesn't like talk about our work, but perhaps someone has addresses and could do exactly that to find the 2nd and last occurrences of " " to isolate city and zip code. Or whatever. Point is, able to imagine it yourself. or not, these things are actually useful to folks.

Not the first time I've seen this question asked in here. For all you askers (let's be honest: "asker" singular) get off. Take a hike. Sneer at us non-programmers as you wish. But do it to our faces someday. That I'd like to participate in.

2018-09-19 13:13:03

Ryn

Am I the only person who wonders why someone would want to identify the Nth occurrence of a character within a text string? I find myself wondering this not just with this tip but with other tips as well.

2018-09-19 08:34:23

Peg Molter

Why did you use SEARCHB rather than SEARCH? I "searched" quite a while on the Internet to find out what SEARCHB was (it's not even listed as part of Excel's functions) and finally found a very slight explanation on the Microsoft site. It has to do with SEARCHB counting 2 bytes per character when using some languages, as opposed to 1 byte per character when using SEARCH. Is there a reason that this should be done in this case?

Thanks.

2016-12-02 14:57:20

Luc

Interesting. I'm looking for a way to find e-mails which contain more than three occurrences of a particular string.

Could I maybe connect Excel to gmail, imort the mail and use a variation of this to find messages which contain e.g. the word 'Pega' more than three times?

2016-11-30 02:56:58

Hai Mai

Thank for your post. It saves my time.

2016-04-13 05:15:35

Michael (Micky) Avidan

@Shakir,
Try the following suggested formula:
(see Figure 1 below)
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL

Figure 1. REPEAT 3 FIRST CHARACTERS 8 TIMES

2016-04-13 03:46:07

shakir babu

How to repeat left 3 characters of the name(SHAKIR) 8 times in Ms excel

2016-04-02 06:08:18

Rick Rothstein

There is a much shorter UDF (a one-liner) available for your FindN function...

Function FindN(sFindWhat As String, sInputString As String, N As Long) As Long
FindN = InStr(Replace(sInputString, sFindWhat, " ", 1, N - 1), sFindWhat)
End Function

2016-02-25 05:17:56

balthamossa2b

@Yanal

This may interest you:

http://www.mrexcel.com/forum/excel-questions/440487-extract-text-number-column-help.html

2016-02-24 08:45:20

YANAL ALKURDI

hi

how can i extract the number between the "#" and the "-" as marked in the red below and for several times
"COST OF XO Ticket(s) # [29]
{ RJ } -- Ticket(s) # 5125838371064 - Pax = REBEKKA ROGGLI
- Ticket(s) # 5125838371065 - Pax = CHRISTOPH RIGGOLI
- Ticket(s) # 5125838371066 - Pax = NAEL ROGGLI CHD
- Ticket(s) # 5125838371067 - Pax = ELA ROGGLI CHD
- Ticket(s) # 5125838371068 - Pax = ALYA ROGGLI INF
- Ticket(s) # 5125838371069 - Pax = ANNA WINKLER
- Route = AMM/SSH/AMM
- Class ="

and if there is a macro to be used.

Thank you, for all your help and hope to fear from you soon.

2015-07-08 09:52:18

Michael (Micky) Avidan

@Gokulraj,
The original task was to find the position of the Nth occurrence of a character in a string AND NOT to find the position of the LAST occurrence.
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2016)
ISRAEL

2015-07-08 01:19:13

Gokulraj

Just Place the below macro in Your excel as a Module Code.

Function LastpositionOfChar(strVal As String, strChar As String)

LongLastpositionOfChar = InStrRev(strVal, strChar)

End Function

2015-06-18 10:58:46

Michael (Micky) Avidan

@Sanjay,
Try the hereunder User Defined Function (UDF):
---------------------------------------
Function Extruct_Numbers_From_Within_Brackets(Rng As Range)
CR = 1
While CR < Len(Rng)
F1 = Application.Find("[", Rng, CR) + 1
F2 = Application.Find("]", Rng, F1) - 1
For N = F1 To F2
NM = NM + Mid(Rng, N, 1)
Next
NM = NM + ";"
CR = F2 + 1
Wend
Extruct_Numbers_From_Within_Brackets = Left(NM, Len(NM) - 1)
End Function
-----------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL

2015-06-17 14:20:02

Sanjay

Thanks You! Please solve my query here: I have a value in a cell A1 which contains:

M4.CTC.VA03.Verify Sales Documents.v2 [14294], MSUP.CTC.VA03.Verify Sales Documents.v2 [14957], MSUP.CTC.VA03.Verify Sales Documents.v2 [15019], MSUP.CTC.VA03.Verify Sales Documents.v3 [15156]

I would like to have a value in Cell B as: 14294;14957;15019;15156

Basically, it should look for a text "[" and then return all characters after untill reaches "]".

2015-02-10 23:02:21

pmor1945

wulflyng

Copy the surrounding text or complete tip to Notepad, and the hidden text will be displayed.

2015-02-10 04:48:40

pmyk

While using in VBA Range should be used.
[code]
Dim Ans As Integer
Ans = FindN("b", Range("C15"), 3)
MsgBox Ans
[/code]

2015-02-09 09:23:17

wulflyng

2015-02-09 04:32:09

balthamossa2b

To make the search non-case sensitive, just add at the top of your VBA Module:

Option Compare Text

##### This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.