Saturday, October 17, 2015

How to get nth occurrence position of a character in text value in Excel ?

To get nth occurrence position of character is very challenging task by using formula in excel. I observed that there are various situation in reporting project where nth occurrence position of character is used for certain data calculations. In such a scenario, it is very difficult to complex excel formula which returns nth position of character for targeted input text.

To resolve such a requirement where nth occurrence position of character needed, I have create a function which will easily give us n position of specific character in targeted text.



Here is Excel Function which will return nth occurrence position of character:


Function Find_N(tFind_What As String, tInput_String As String, N As Integer) As Integer
' Author: Dreams24
' Written for VBA Tricks and tips blog
https://vbatricksntips.com

Dim i As Integer
Application.Volatile

Find_N = 0


For i = 1 To N

Find_N = InStr(Find_N + 1, tInput_String, tFind_What)
If Find_N = 0 Then Exit For
Next i
End Function

No comments: