珍娱客>科技>VBA 收集 - 统计字符串出现次数>正文

VBA 收集 - 统计字符串出现次数

历史2022-06-170 次阅读

Function 统计字符串出现次数(sourceStr As String, searchStr As String) As Long
On Error GoTo Error_Handler

    统计字符串出现次数 = UBound(Split(sourceStr, searchStr))
    
Error_Handler_Exit:
    On Error Resume Next
    Exit Function
    
Error_Handler:
    Resume Error_Handler_Exit
    
End Function

测试


Sub test()
    msg = "出现 " & 统计字符串出现次数("大家好,我是笨笨,笨笨的笨,笨笨的笨,谢谢!", "笨") & " 次"
    MsgBox msg, vbOKOnly + vbInformation, "统计出现次数"
End Sub
VBA 收集 - 统计字符串出现次数

Function 统计字符串出现次数(sourceStr As String, searchStr As String) As Long

On Error GoTo Error_Handler

统计字符串出现次数 = UBound(Split(sourceStr, searchStr))

Error_Handler_Exit:

On Error Resume Next

Exit Function

Error_Handler:

Resume Error_Handler_Exit

End Function