Find the nth occurrence of a character in Excel – 3 quick ways
Finding the nth occurrence of a specific character within a text string in Excel can be particularly useful in data analysis, where you might need to parse strings or extract information based on certain delimiters or patterns. For instance, let's find the 2nd or 3rd occurrence of the character "-" in a text string. I'll demonstrate straightforward techniques to efficiently accomplish this task.
Find nth occurrence of a character in text string with formula
You can craft a formula to find the nth occurrence of a character. Please do as this:
1. Enter or copy the following formula into a cell where you want to get the result:
=FIND(CHAR(1),SUBSTITUTE(A2,"-",CHAR(1),2))
- A2: The cell that contains the string.
- -: The character you are looking for.
- 2: The 2nd occurrence you want to find, you can change it to 3,4…as you need.
2. Then, drag the formula fill down to fill the formula to other cells, and the 2nd position of the character – are displayed at once, see screenshot:
- SUBSTITUTE replaces the nth occurrence of the character with a non-printable character (CHAR(1)).
- FIND searches for this non-printable character, giving the position of the nth occurrence.
Find nth occurrence of a character in text string with Kutools for Excel
If you're not a fan of using formula or VBA, you might consider a convenient alternative – Kutools for Excel. Within its Formula groups, you'll find a useful utility – Find where the character appears Nth in a string which quickly identifies and returns the nth position of any character in a cell.
After installing Kutools for Excel, click Kutools > Formula Helper > Formula Helper to open the Formula Helper dialog box. Click a cell where you want to put the result. Then, do as follows:
- Select Lookup from the drop-down list of Formula Type section;
- Choose Find where the character appears Nth in a string in Choose a formula section;
- Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the Arguments input section;
- At last, click OK button to get the result.
Find nth occurrence of a character in text stringl with User Defined Function
In this section, we'll explore how to create and use a UDF to find the nth occurrence of a character in Excel, providing a step-by-step guide to help you optimize your data handling.
- Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
- Click Insert > Module, and paste the following macro in the Module Window.
Function FindN(sFindWhat As String, _ sInputString As String, N As Integer) As Integer 'Updateby Extendoffice 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
- Then, close the vba window. Go back to the worksheet, enter the following formula into a cell, and then drag the fill handle down to fill the formula to other cells, see screenshot:
=FindN("-",A2,3)
- Count the number of occurrences of a word in a column
- If you have a column data which includes some duplicate values as below screenshot shown, and now what you want to do is to count the number of the occurrences of a specific word in this column. Now with this tutorial, I introduce some tricks to solve it quickly in Excel.
- Replace first n character or nth occurrence of character with another
- In Excel, we usually find and replace a string by the Find and Replace function, but if you are in one of below cases, the Find and Replace function cannot help you.
- Extract a nth character from a string
- In general, you may want to extract string after a specific character, but in this case, I want to extract the nth character from a string as below screenshot shown.
- Extract first/last n characters from string
- For instance, there is a list with long strings in each cell, and you want to extract the first n characters from each string only, like the first 3 characters of each string, and now you can use the following methods to solve it in Excel.
Related Articles:
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!