Vba 计算选定行/单元格中的单词字符数

Vba 计算选定行/单元格中的单词字符数,vba,ms-word,character,cell,Vba,Ms Word,Character,Cell,我必须计算有表格的单词选择中的字符数。我发现了以下VBA代码: Sub CountCharacters() Dim Title As String Dim CharCount As Integer Dim Message As String Title = "CharCount" CharCount = Len(Selection) Message = LTrim(Str(CharCount)) + " character" If Cha

我必须计算有表格的单词选择中的字符数。我发现了以下VBA代码:

Sub CountCharacters()
    Dim Title As String
    Dim CharCount As Integer
    Dim Message As String

    Title = "CharCount"
    CharCount = Len(Selection)
    Message = LTrim(Str(CharCount)) + " character"
    If CharCount <> 1 Then Message = Message + "s"
    MsgBox Message, vbOKOnly, Title
End Sub
Sub CountCharacters()
将标题设置为字符串
Dim CharCount为整数
将消息设置为字符串
Title=“CharCount”
CharCount=Len(选择)
Message=LTrim(Str(CharCount))+“字符”
如果字符数为1,则消息=消息+“s”
MsgBox消息,仅vbOKOnly,标题
端接头
问题是它只计算一个选定单元格的字符数。 如何将其更改为同时计算其他单元格中的字符数

谢谢

选项显式
Option Explicit

Sub CountCharacters()
    Dim Title As String
    Dim CharCount As Integer
    Dim Message As String

    Title = "CharCount"

    'To select the table in which the cursor is place.
    'NOTE: if the cursor is not in a table is will give an error
    Selection.Tables(1).Select

    CharCount = Selection.Range.ComputeStatistics(wdStatisticCharacters)

    Message = LTrim(Str(CharCount)) + " character"
    If CharCount <> 1 Then Message = Message + "s"
    MsgBox Message, vbOKOnly, Title


    'Otherwise you can use:
        'Note that the "Cell End Markers" are seen as two chracters Chr(13) & Chr(7).
        'You will then need to subtract the  number of cells multiplied by 2.
        'You also need to take into account that those cell end markers are in _
            each line outside the table as well.
        'There is also a Selection.Tables(1).Range.Cells.Count but this will omit _
            the additional "Column" of cell end markers
        CharCount = Len(Selection) - (Selection.Tables(1).Rows.Count * _
                                     (Selection.Tables(1).Columns.Count + 1)) * 2

    Message = LTrim(Str(CharCount)) + " character"
    If CharCount <> 1 Then Message = Message + "s"
    MsgBox Message, vbOKOnly, Title

End Sub
子计数字符() 将标题设置为字符串 Dim CharCount为整数 将消息设置为字符串 Title=“CharCount” '以选择放置光标的表。 '注意:如果光标不在表中,将显示错误 选择。表(1)。选择 CharCount=Selection.Range.ComputeTestatics(wdStatisticCharacters) Message=LTrim(Str(CharCount))+“字符” 如果字符数为1,则消息=消息+“s” MsgBox消息,仅vbOKOnly,标题 '否则,您可以使用: 请注意,“细胞末端标记”被视为两个Chr(13)和Chr(7)。 '然后需要将单元格数乘以2。 '您还需要考虑到这些细胞末端标记位于_ 桌子外面的每一行也是一样。 '还有一个Selection.Tables(1).Range.Cells.Count,但这将忽略_ 单元格结束标记的附加“列” CharCount=Len(Selection)-(Selection.Tables(1).Rows.Count*_ (Selection.Tables(1.Columns.Count+1))*2 Message=LTrim(Str(CharCount))+“字符” 如果字符数为1,则消息=消息+“s” MsgBox消息,仅vbOKOnly,标题 端接头
选项显式
子计数字符()
将标题设置为字符串
Dim CharCount为整数
将消息设置为字符串
Title=“CharCount”
'以选择放置光标的表。
'注意:如果光标不在表中,将显示错误
选择。表(1)。选择
CharCount=Selection.Range.ComputeTestatics(wdStatisticCharacters)
Message=LTrim(Str(CharCount))+“字符”
如果字符数为1,则消息=消息+“s”
MsgBox消息,仅vbOKOnly,标题
'否则,您可以使用:
请注意,“细胞末端标记”被视为两个Chr(13)和Chr(7)。
'然后需要将单元格数乘以2。
'您还需要考虑到这些细胞末端标记位于_
桌子外面的每一行也是一样。
'还有一个Selection.Tables(1).Range.Cells.Count,但这将忽略_
单元格结束标记的附加“列”
CharCount=Len(Selection)-(Selection.Tables(1).Rows.Count*_
(Selection.Tables(1.Columns.Count+1))*2
Message=LTrim(Str(CharCount))+“字符”
如果字符数为1,则消息=消息+“s”
MsgBox消息,仅vbOKOnly,标题
端接头

非常感谢,它很有效!这个解释也很有帮助。@LeFunk,记住把答案标记为正确并投弃权票。享受StackOverflow非常感谢,它很有效!这个解释也很有帮助。@LeFunk,记住把答案标记为正确并投弃权票。享受堆栈溢出