打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

excelperfect

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

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

欢迎在下面留言,完善本文内容,让更多的人学到更完美的知识。

欢迎到知识星球:完美Excel社群,进行技术交流和提问,获取更多电子资料。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
怎样将PPT文件转换为Word文档?
VBA.vbcrlf
word中功能超强的查找与替换
Excel应用大全
系统地学习Excel第15课,「查找与替换」对话框中的「查找」功能
基础篇--掌握查找和替换,月薪涨到一两万
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服