# Finding the Nth Occurrence of a Character

Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Finding the Nth Occurrence of a Character.

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

Application.Volatile
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)
```

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3324) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Finding the Nth Occurrence of a Character.

Related Tips:

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

Michael (Micky) Avidan    05 Feb 2015, 07:55
@Jason,
If I understood you correctly and the pattern of the string stays constant and you prefer to retrieve the 4th. part (after the 3rd. space) - you might consider using a much shorter formula:
=MID(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1)*3,LEN(A1))*1
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL
Jason    04 Feb 2015, 19:10
The section "a more flexible formula would be" helped me a great deal!

After hours of searching I finally found a solution to take "88.88" as a number out of the following sample text:

"2015-02-03 77.77 GB 88.88 GB 99.99 GB "

My sample formula ended up being:
=(VALUE((MID(\$B46,(FIND(CHAR(1),SUBSTITUTE(\$B46," ",CHAR(1),3))+1),(FIND(CHAR(1),SUBSTITUTE(\$B46," ",CHAR(1),4)))-(FIND(CHAR(1),SUBSTITUTE(\$B46," ",CHAR(1),3))+1)))))

As it's being copied across from a RDP session, it comes as plain text.

Thank you so much!
Michael    22 Oct 2014, 15:53
Clear, concise, and works perfectly.

Thank you for this.
Karl    30 Apr 2013, 09:32
Thanks for this. I've seen the tips before (except using FIND instead of SEARCH), but this explains them better.
Tamara    24 Nov 2011, 23:21
Thanks a lot for this tip. I've looking around for a simple formula for this and all of them were messy. Your formula is easy and effective. Thanks again!

 *Name: Email: Notify me about new comments ONLY FOR THIS TIP Notify me about new comments ANYWHERE ON THIS SITE Hide my email address *Text: *What is 4+5 (To prevent automated submissions and spam.)

# Our Company

Sharon Parq Associates, Inc.

# Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Excel Products

Word Products

# Our Authors

Author Index

Write for Tips.Net