Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to find the first, last or nth occurrence of a character in Excel?

doc-get-last-position-char-1-1

Supposing you have the following list of text strings which including the character “-”, and now you want to get the last or nth occurrence of the character “-”, have you any good ideas to deal with this problem?

Find the last occurrence of character with formulas

Find the last occurrence of character with User Defined Function

Find the first or nth occurrence of specific character with Kutools for Excel


Find the first or nth occurrence of a specific character:

Kutools for Excel’s Find nth occurrence of a character feature may help you get the first or nth position of a specific character quickly and easily.

doc-get-last-position-char-1

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!



Here are some formulas can help you to find the last position of a specific character, please do as follows:

1. Next to your text string, please enter this formula =SEARCH("^^",SUBSTITUTE(A2,"-","^^",LEN(A2)-LEN(SUBSTITUTE(A2,"-","")))), this example, I will enter it to cell C2,and press Enter key to get the result see screenshot:

doc-get-last-position-char-1

2. Then drag the fill handle over to the range that you want to apply this formula, and you will get the last occurrence of the specific character “-”, see screenshot:

doc-get-last-position-char-1

Notes:

1. You can use the following formula to solve this job as well: =LOOKUP(2,1/(MID(A2,ROW(INDIRECT("1:"&LEN(A2))),1)="-"),ROW(INDIRECT("1:"&LEN(A2))))

2. In the above formulas, A2 indicates the cell value which you want to use. And “-” stands for the specific character you want to get its last position, you can change it to any other characters as your need.


To get the position of the last character “-”, you can also create User Defined Function, do as this:

1. Open your worksheet that you want to use.

2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications Window.

3. Then click Insert > Module, and paste the following macro in the Module window.

VBA code: find the last occurrence of character

Function LastpositionOfChar(strVal As String, strChar As String) As Long
LastpositionOfChar = InStrRev(strVal, strChar)
End Function

4. Then save and close this code, go back to the worksheet, and enter this formula =lastpositionofchar(A2,"-") (A2 is the cell which contains the data you want to use, and “-” is the character you need to find its last position, you can modify them as your needing.) into a blank cell besides your data, see screenshot:

doc-get-last-position-char-1

5. And then drag the fill handle to the range you need to apply this formula, and the occurrence of last character “-” have been extracted from the text strings as following screenshot:

doc-get-last-position-char-1


Maybe, sometimes, you want to find and get the first or nth occurrence of a specific character, in this case, above methods will not work. But, with Kutools for Excel’s Find nth occurrence of a character utility, you can solve this task as quickly as you can.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days

For example, I want to get the second occurrence of the character “-”, please do as follows:

1. Click a cell where you want to put the result.

2. Then click Kutools > Formulas > Find nth occurrence of a character, see screenshot:

doc-get-last-position-char-1

3. In the Formula Helper dialog box:

(1.) Select the cell that you want to use beside the String text box;

(2.) Enter the character that you want to find and get the position beside the Character text box; (Note: the character you entered need to be enclosed with quotes.)

(3.) At last, specify the number which you want to get the position of the character beside the Nth occurrence box.

doc-get-last-position-char-1

4. Then click Ok button, and then drag the fill handle down to the cells that you want to get the position to get the following result as you need.

doc-get-last-position-char-1

Download and free trial Kutools for Excel Now !


Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Related articles:

How to extract first letter of each word from cell?

How to extract first / last / nth word from text string in Excel?


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd

Would you like to complete your daily work quickly and perfectly? Kutools for Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for you.

  • Designed for 1500+ work scenarios, helps you solve 80% Excel problems.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands.
  • Become an Excel expert in 3 minutes. No longer need to remember any painful formulas and VBA codes.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Being used by 110,000 elites and 300+ well-known companies.

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Anders · 2 years ago
    Thank You, great work !!

    Best Regards Anders
  • To post as a guest, your comment is unpublished.
    Tom · 2 years ago
    Thank you for these formulas. Very helpful.