Option Explicit
'對當前單元格中的文本進行字數統計
Sub TotalCellCharNum()
Dim i As Long
i = Len(ActiveCell.Value)
MsgBox "當前單元格的字數為:" & Chr(10) & i
End Sub
'對所選的單元格區域中的文本進行字數統計
Sub TotalSelectionCharNum()
Dim i As Long
Dim rng As Range
For Each rng In Selection
i = i + Len(rng.Value)
Next rng
MsgBox "所選單元格區域的字數為:" & Chr(10) & i
End Sub
'對當前單元格中的文本分類進行字數統計
Sub SubTotalCellCharNum()
Dim str As String, ChineseChar As Long
Dim Alphabetic As Long, Number As Long
Dim i As Long, j As Long
j = Len(ActiveCell.Value)
For i = 1 To Len(ActiveCell)
str = Mid(ActiveCell.Value, i, 1)
If str Like "[一-龥]" = True Then
ChineseChar = ChineseChar + 1 '漢字累加
ElseIf str Like "[a-zA-Z]" = True Then
Alphabetic = Alphabetic + 1 '字母累加
ElseIf str Like "[0-9]" = True Then
Number = Number + 1 '數字累加
End If
Next
MsgBox "當前單元格中共有字數" & j & "個,其中:" & vbCrLf & "漢字:" & ChineseChar & "個" & _
vbCrLf & "字母:" & Alphabetic & "個" & _
vbCrLf & "數字:" & Number & "個", vbInformation, "文本分類統計"
End Sub
'對所選的單元格區域中的文本分類進行字數統計
Sub SubTotalSelectionCharNum()
Dim str As String, ChineseChar As Long
Dim Alphabetic As Long, Number As Long
Dim i As Long, rng As Range, j As Long
For Each rng In Selection
j = j + Len(rng.Value)
For i = 1 To Len(rng)
str = Mid(rng.Value, i, 1)
If str Like "[一-龥]" = True Then
ChineseChar = ChineseChar + 1 '漢字累加
ElseIf str Like "[a-zA-Z]" = True Then
Alphabetic = Alphabetic + 1 '字母累加
ElseIf str Like "[0-9]" = True Then
Number = Number + 1 '數字累加
End If
Next
Next
MsgBox "所選單元格區域中共有字數" & j & "個,其中:" & vbCrLf & "漢字:" & ChineseChar & "個" & _
vbCrLf & "字母:" & Alphabetic & "個" & _
vbCrLf & "數字:" & Number & "個", vbInformation, "文本分類統計"
End Sub