我刚刚开始在excel中尝试一些VBA,并且我试图计算选择的注释中有多少个特定单词出现。这是我到目前为止所做的:
Function CountStringInComments(strText As String, ByVal Target As Range) As Long
Dim c As Comment
Dim n As Long
For Each c In Target.Comments
n = n - (InStr(1, c.Text, strText, vbTextCompare) > 0)
Next
CountStringInComments = n
Set c = Nothing
End Function
并且调用我的函数应该如下所示:
=CountStringInComments("bruit",Z55:Z58)
我得到一个#VALUE!错误。
我不熟悉VB,所以感谢任何帮助。感谢。
答案 0 :(得分:2)
您需要记录单元格而不是注释,然后搜索该单元格的注释文本。
Function CountStringInComments(strText As String, ByVal Target As Range) As Long
Dim c As Range
Dim n As Long
For Each c In Target.Cells
If Not c.Comment Is Nothing Then
n = n - (InStr(1, c.Comment.Text, strText, vbTextCompare) > 0)
End If
Next
CountStringInComments = n
Set c = Nothing
End Function
答案 1 :(得分:0)
如果你想计算所有比赛(包括一次评论中的多次),那么你可以这样做:
Function CountStringInComments(str As String, ByVal r As Range) As Long
Dim re As RegExp
Set re = New RegExp
re.Global = True
re.Pattern = str
re.IgnoreCase = True
Dim cell As Range
For Each cell In r
If Not cell.Comment Is Nothing Then
Dim mColl As MatchCollection
Set mColl = re.Execute(cell.Comment.Text)
CountStringInComments = CountStringInComments + mColl.Count
End If
Next cell
End Function
需要引用 Microsoft VBScript正则表达式5.5