新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Sub Command1_Click()
Debug.Print f_CountTimes("a1b12c1d12e", "12")
End Sub
Private Function f_CountTimes(strW$, strFindWord$) As Long
' 统计abc出现次数
Dim lngCount&, lngPos&
lngPos = InStr(strW, strFindWord)
Do While lngPos > 0
lngCount = lngCount + 1
lngPos = InStr(lngPos + 1, strW, strFindWord)
Loop
f_CountTimes = lngCount
End Function
运行结果:
2