How to Find the Last Occurrence of a Character in a String in Excel

Data analysis often requires us to parse texts in different ways. You might need to clean some data, extract some words, or validate a string. As there is no direct function in excel to find the last occurrence of a character in a string, you need to get creative. In this article, we will learn five ways to find last occurrence of characters in string in excel.

Key Takeaways

In a helper cell, write this formula:
=LEN(TEXTBEFORE(A2,”:”,-1))+1
Replace A2 with your input cell, “:” with your desired character, and Press Enter.

overview image

That was one way of doing that. However, for flexibility, you might want to learn some more ways to do that. In this article, we will present a total of five methods for doing the job. Therefore, consider reading the whole article to understand the procedures and explanations.

Download Practice Workbook
1

Finding Last Occurrence of Character Using the LEN-TEXTBEFORE Functions

In this sample, we have a dataset containing some records of students, including their subject names. The administration wants to parse these records programmatically later. The subject names and the student names are separated using a colon (:). In order to separate those, we need to find the position of the colon.

This formula is probably the smallest one in the whole article. We are going to use only two functions and find the exact position of the desired character. The steps to be followed are below:

Select the cell where you want to get the last occurrence location.
Enter this formula:

=LEN(TEXTBEFORE(A2,”:”,-1))+1

Explanation
Here, the TEXTBEFORE function looks at the A2 cell and returns the text before the last occurrence of the colon (:). Then the LEN function returns the length of that text. At the end, we add 1 to that length because the length we got before was excluding the desired character, and we have to reinclude that.

Using the LEN-TEXTBEFORE Functions


2

Using a Combination of MATCH-MID-SEQUENCE-LEN Function

We are going to use a formula that is a bit more complex than before. There will be four functions in the work today, and we will learn how those work together for our desired output.

Just like before, select a cell, and put this formula in:

=MATCH(2,1/(MID(A2,SEQUENCE(LEN(A2)),1)=”:”))

Explanation
First, the SEQUENCE function creates an array from the length of A2, given by the LEN function. Then the MID function creates another array of the characters from the A2 cell. The parameters for that function are the input cell, the array, and the number of characters it needs to extract. The 1 at the beginning of MID and the “:” at the end check whether the character is the desired one or not. Finally, the MATCH function looks for 2, which is not there, so it will return the last 1 in the array. That 1 is the position of the last (:) in the array.

Press Enter after replacing A2 with your string/input cell, and “:” with your character.

Using MATCH-MID-SEQUENCE-LEN


3

Applying Another Formula with FIND-SUBSTITUTE-LEN Functions

This method is something we will not recommend you use for every case. The functions replace the desired character with something else to do the job. However, if you already have the replacement character in the string, the formula fails miserably. Only use this method if you have to.

Paste this formula in the helper cell:

=FIND(“@”,SUBSTITUTE(A2,”:”,”@”,(LEN(A2)-LEN(SUBSTITUTE(A2,”:”,””)))))

Explanation
The SUBSTITUTE and LEN functions extract the length of the string without the character “:”. Then the last instance of “:” is replaced with @. Remember, if your string already has @, you must use some other character here that has no occurrence in your string. Finally, the location of @ is returned in the output cell.

Do not forget to replace A2 with your input cell and “:” with your character.

Using FIND-SUBSTITUTE-LEN


4

Using MAX-IF-MID-ROW-INDIRECT-LEN Functions

This is going to be the largest formula in this whole article. If you want to understand the formula, read the explanation part carefully. If you don’t, just replace the input cell and the desired character, and you will be good to go.

Place this function in the output cell:

=MAX(IF(MID(A2, ROW(INDIRECT(“1:” & LEN(A2))), 1) = “:”, ROW(INDIRECT(“1:” & LEN(A2))), 0))

Press Enter.

Using MAX-IF-MID-ROW-INDIRECT-LEN Functions


5

Running VBA Code

Tired of all these functions? We are now making things a bit more interesting. We are going to use some visual basic coding to accomplish the job. Follow the steps below:

Press Alt+F11 to open the code editor.
From the top menubar, go to Insert > Module.
Write this code in the window:

Public Function GetLastCharacterPosition(inputText As String, targetChar As String) As Integer
Dim position As Integer
position = -1
Dim index As Integer
For index = Len(inputText) To 1 Step -1
If Mid(inputText, index, 1) = targetChar Then
position = index
Exit For
End If
Next index
GetLastCharacterPosition = position
End Function

Using VBA Code

Go back to the spreadsheet, and write this code:

=GetLastCharacterPosition(A2, “:”)

Replace A2 with your input cell and “:” with your desired character.
Press Enter.


Frequently Asked Questions

How to extract last 5 characters in Excel?

Use this formula:
=RIGHT(A1,5)
Replace A1 with your input cell.

How to get first 10 characters from string in Excel?

Type this formula:
=LEFT(A1,10)
Here, A1 is the input cell.

What is the value of the last character in a string?

It is the length of the string minus 1. In excel you can put this:
=LEN(A1) – 1
A1 will be the input string cell.

How to extract middle 4 characters in Excel?

This formula should suffice:
=MID(A1, LEN(A1)/2 -2  ,4)
Here, A1 is the input cell. The MID function takes the input cell, the point where it should start extracting, which is determined using the LEN function, and how many characters it needs to extract.

How do I separate text in Excel?

From the menu of excel, go to Data>Text to Columns. Then the Convert Text to Columns wizard will guide you through the process of separating text into different columns.


Wrapping Up

In this article, we have learned five ways to find the last occurrence of a character in a string using excel. The practice file contains all of the methods used in this article. Feel free to use the file and leave a suggestion below for us on how we can improve.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo
Verified by MonsterInsights