get the the character instance of the 1st character of a sub-sting in a string

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello,

I need to get the character instance of the 1st character of a sub-string in a string

I can get the character instance of the entire string but not the 1st character of a sub-string in a string

Ex
ref_value.value = sjdhfss jj sd (ppo nns) GGG
sub-string = sd (ppo nns)

the first instance of substring = s
its instance = 4

Thanks

Code:
Function COUNTTEXT(ref_value As Range, ref_string As String) As Long


Dim i As Integer, count As Integer


count = 0
If Len(ref_string) <> 1 Then COUNTTEXT = CVErr(xlErrValue): Exit Function
For i = 1 To Len(ref_value.value)
    If Mid(ref_value, i, 1) = ref_string Then count = count + 1
Next


COUNTTEXT = count


End Function
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Does this do what you want?


Excel 2013/2016
DEFG
5sjdhfss jj sd (ppo nns) GGG4
6sd (ppo nns)
Sheet1
Cell Formulas
RangeFormula
G5=INSTR_SUBSTR(D5,D6)


Code:
Function INSTR_SUBSTR(ByVal sMain As String, sSub As String) As Long
Dim iSubStrPos As Long
Dim firstChar  As String
Dim iCounter   As Long
Dim iPos       As Long


iSubStrPos = InStr(1, sMain, sSub)


If iSubStrPos = 0 Then
    INSTR_SUBSTR = 0
Else
    firstChar = Left$(sSub, 1)
    sMain = Left$(sMain, iSubStrPos - 1)
    
    iPos = InStr(1, sMain, firstChar)
    iCounter = 0
    Do Until iPos = 0
        iCounter = iCounter + 1
        iPos = InStr(1 + iPos, sMain, firstChar)
    Loop
    INSTR_SUBSTR = iCounter + 1
End If
End Function
 
Upvote 0
Or a formula ...

A​
B​
C​
2​
sjdhfss jj sd (ppo nns) GGG
4​
B2: =LEN(LEFT(A2, FIND(A3, A2))) - LEN(SUBSTITUTE(LEFT(A2, FIND(A3, A2)), LEFT(A2), ""))
3​
sd (ppo nns)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,231
Messages
6,129,631
Members
449,522
Latest member
natalia188

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