下面一个利用InStr函数计算字符串出现的次数的函数:Public Function CountSubstrings(target As String, _
    template As String, CaseSensitive As Boolean) _
    As Integer'   Returns the number of times template occurs in target.
'   Returns -1 if either string is blank or if templateis longer than target. If CaseSensitive is true, performs a case-sensitive comparison for text. If false, the comparison is case-insensitive.
Dim pos1 As Integer, pos2 As Integer, count As IntegerIf Len(target) = 0 Or Len(template) = 0 Or _
  Len(template) > Len(target) Then
    CountSubstrings = -1
    Exit Function
 End If count = 0
 pos2 = 1
    
Do
  If CaseSensitive Then
    pos1 = InStr(pos2, target, template, vbBinaryCompare)
  Else
    pos1 = InStr(pos2, target, template, vbTextCompare)
  End If
  If pos1 > 0 Then
    count = count + 1
    pos2 = pos1 + 1
  End If
Loop Until pos1 = 0CountSubstrings = countEnd Function
我现在有两个文本框 Text1 和 Text2 ,如何调用此函数,使得可以统计出 Text2 中出现了几次 Text1 中的内容,谢谢了

解决方案 »

  1.   

    count = CountSubstrings(Text1.Text, Text2.Text,True/False)
    True还是False自己看着办吧
      

  2.   

    错了,反了,应该是count = CountSubstrings(Text2.Text, Text1.Text,True/False) 
      

  3.   

    count = CountSubstrings(Text2.Text, Text1.Text,False)
      

  4.   

    不行呀,出现错误了,错误指向“count =”,错误提示是:编译错误:
    函数或接口标记为限制的, 或函数使用了 Visual Basic 中不支持的自动类型
      

  5.   

    搞定了,count可能是关键字吧,换个名就行了,谢谢!
      

  6.   

    pos2 = pos1 + 1 
    这部分我觉得最好还是再优化一下,比如:
    pos2 = pos1 + len(template)