前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA实用小程序78:统计工作簿内文本框和批注中的字符及单词数

VBA实用小程序78:统计工作簿内文本框和批注中的字符及单词数

作者头像
fanjy
发布2021-06-01 11:14:17
1K0
发布2021-06-01 11:14:17
举报
文章被收录于专栏:完美Excel

下面的程序可以统计工作簿所有工作表中文本框和批注内的字符和单词的数量。

代码语言: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论坛,供有兴趣的朋友学习参考。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 完美Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档