UDF Get the position of the first character of sub-string of string

userxyz

New Member
Joined
Feb 18, 2018
Messages
5
Hello, new to vba and I need help writting a UDF

I need a macro to get the position of the first character of a repeated sub-string of a string

If cell value is "The green cat something1 the green cat something2 in ... green cat somethingM"

Sub-string is the nth instance of "green cat"

How to get the position number of the g in the full string

Thanks for any help on this
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
something along these lines i assume is what you are after


Excel 2013/2016
A
1The green cat something1 the green cat something2 in ... green cat somethingM"
2green cat
358
Sheet1
Cell Formulas
RangeFormula
A3=GetNthPosition(A1,A2,3)


Code:
Function GetNthPosition(strToSearch As String, _
                        strMatch As String, _
                        nTh As Long) As Long
Dim j As Long, i As Long
j = 0
i = InStr(1, strToSearch, strMatch)
Do Until i = 0
    j = j + 1
    If j = nTh Then GetNthPosition = i: Exit Function
    i = InStr(1 + i, strToSearch, strMatch)
Loop
GetNthPosition = 0
End Function
 
Upvote 0
Thank you! that is what I needed

May I ask a follow up question?

Say the sub-string was not repeated just a random sub-string of a string how would I then get the position of the first character of a NON- repeated sub-string of a string

Thank you again for you
 
Upvote 0
You would use 1 as the third argument of the above function.

Thank you! that is what I needed

May I ask a follow up question?

Say the sub-string was not repeated just a random sub-string of a string how would I then get the position of the first character of a NON- repeated sub-string of a string

Thank you again for you
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top