下面的程序可以统计工作簿所有工作表中文本框和批注内的字符和单词的数量。
代码语言:javascript复制Sub CountCharWorBOXCMT()
Dim wks As Worksheet
Dim lCommentch As Long
Dim lCommentwords As Long
Dim lTxtBoxChar As Long
Dim lTxtBoxCharWords As Long
Dim objShp As Shape
Dim sMsg As String
Dim vbaArray
Application.ScreenUpdating = False
lCommentch = 0
lCommentwords = 0
lTxtBoxChar = 0
lTxtBoxCharWords = 0
For Each wks In ActiveWorkbook.Worksheets
For Each objShp In wks.Shapes
' 统计文本框中的字符和单词
If objShp.Type = 17 Then
IfLen(objShp.TextFrame2.TextRange.Characters.Text) <> 0 Then
lTxtBoxChar = lTxtBoxChar objShp.TextFrame.Characters.Count
vbaArray =VBA.Split(objShp.TextFrame2.TextRange.Characters.Text, " ")
lTxtBoxCharWords =lTxtBoxCharWords UBound(vbaArray) 1
End If
End If
' 统计批注中的字符和单词
If objShp.Type = 4 Then
With objShp.TextFrame.Characters
lCommentch = lCommentch Len(.Text)
vbaArray = VBA.Split(.Text," ")
lCommentwords =lCommentwords UBound(vbaArray) 1
End With
End If
Next objShp
Next wks
sMsg = "文本框中的字符数: " & Format(lTxtBoxChar,"### ### ##0") & vbCrLf
sMsg = sMsg & "文本框中的单词数: " &Format(lTxtBoxCharWords, "### ### ##0") & vbCrLf & vbCrLf
sMsg = sMsg & "批注中的字符数: " &Format(lCommentch, "### ### ##0") & vbCrLf
sMsg = sMsg & "批注中的单词数: " &Format(lCommentwords, "### ### ##0") & vbCrLf & vbCrLf
MsgBox Prompt:=sMsg, Title:="统计汇总"
Application.ScreenUpdating = True
End Sub
在示例工作簿中运行后的效果如下图1所示。
图1
小结:
1. 注意遍历工作表中特定形状的程序代码。
2. 注意统计单词的代码技巧。
注:本程序整理自ozgrid.com论坛,供有兴趣的朋友学习参考。