程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> VB對所選的單元格區域中的文本進行字數統計

VB對所選的單元格區域中的文本進行字數統計

編輯:VB綜合教程
 

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
 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved