ExcelRibbon.Tips.Net ExcelTips (Ribbon Interface)

Finding the Nth Occurrence of a Character

Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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.

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:


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:


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
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:


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

Related Tips:

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!


Comments for this tip:

Michael (Micky) Avidan    08 Jul 2015, 09:52
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)
Gokulraj    08 Jul 2015, 01:19
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
Michael (Micky) Avidan    18 Jun 2015, 10:58
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)
               NM = NM + ";"
         CR = F2 + 1
    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)
Sanjay    17 Jun 2015, 14:20
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 "]".

Please help.
pmor1945    10 Feb 2015, 23:02

Copy the surrounding text or complete tip to Notepad, and the hidden text will be displayed.
pmyk    10 Feb 2015, 04:48
While using in VBA Range should be used.
Dim Ans As Integer
 Ans = FindN("b", Range("C15"), 3)
 MsgBox Ans
wulflyng    09 Feb 2015, 09:23
On my system, an advertisement covers part of the first formula.
balthamossa2b    09 Feb 2015, 04:32
To make the search non-case sensitive, just add at the top of your VBA Module:

Option Compare Text

Leave your own comment:

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

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Our Products

Premium Newsletters

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2015 Sharon Parq Associates, Inc.