问题描述:
下面是别人用VB编辑器编的自编函数谁能够帮我解释一下意思.
打开vb编辑器 然后插入模块,在模块1中插入函数
Public Function tj(t As String, m As String) As Integer
Dim i As Integer, k As Integer, s As String, s1 As String, cont As Integer
s = m
cont = 0
k = Len(t)
If k > 2 Then
For i = 1 To k
s1 = Mid(t, i, 1)
If s1 = s Then cont = cont + 1
Next i
tj = cont
Else: tj = 0
End If
End Function
使用时你只需要象使用excel函数一样就行,如你想计算A2单元格内的"+",可在任一空单元格中输入: =tj(a2,"+")就行了,不管有多少个"+"都没关系,你要统计其他字符,只需把"+"换成其他字符就行如果你只需要统计加号,可简化为
Public Function tj(t As String) As Integer
Dim i As Integer, s1 As String, k As Integer, cont As Integer
cont = 0
k = Len(t)
If k > 2 Then
For i = 1 To k
s1 = Mid(t, i, 1)
If s1 = "+" Then cont = cont + 1
Next i
tj = cont
Else: tj = 0
End If
End Function
使用时就用: =tj(a2)就行了
问题解答:
统计某字符出现的次数
其实用Excel的函数就可以做到了
A1:1+2+3+4
A2:+
=(len(A1)-len(subtitute(A1,A2,"")))/len(A2)
前面的是统计S个数后面的是统计+个数,没有什么难的.