Function StringCount(strSource As String, strSub As String) As Long
Dim lngSrc As Long
lngSrc = Len(strSource)
Dim lngFor As Long
Dim lngCnt As Long
For lngFor = 1 To lngSrc
If Mid(strSource, lngFor, Len(strSub)) = strSub Then lngCnt = lngCnt + 1
Next
StringCount = lngCnt
End Function
Dim lngSrc As Long
lngSrc = Len(strSource)
Dim lngFor As Long
Dim lngCnt As Long
For lngFor = 1 To lngSrc
If Mid(strSource, lngFor, Len(strSub)) = strSub Then lngCnt = lngCnt + 1
Next
StringCount = lngCnt
End Function
debug.print StringCount("abcdabefgab","ab") ====>结果是3
debug.print StringCount("abcdabefgab","a") ====>结果也是3
'可以使用总长度减去去掉子字串后的长度,然后除以字串本身长度,就得到次数了
Debug.Print (Len(strSource) - Len(Replace(strSource, strSub, ""))) / Len(strSub)
Function StringCount(strSource As String, strSub As String) As Long
dim buff() as string
if lenstr(strsub)=0 then exit function
buff=split(strSource,strSub)
StringCount=ubound(buff)+1
End Function
winehero(编程人生) 的REPLACE大法是一种技巧,LZ可以参考。