|
- Option Explicit
- Sub Test()
- Dim lngRows As Long
- Dim arr As Variant, strTemp As String
-
- lngRows = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
- arr = Sheet1.Range("A1:A" & lngRows)
-
- '不知道为什么数组参数的Index在你的XLS中无效,只能循环拼装所有的单元格了
- 'arr = Application.WorksheetFunction.Index(arr, 0, 1)
-
- For lngRows = LBound(arr) To UBound(arr)
- strTemp = strTemp & arr(lngRows, 1)
- Next
-
- arr = CountChar(strTemp)
-
- Sheet1.Range("C1").Resize(3, 1) = arr
- End Sub
- Function CountChar(strVal As String) As Variant
- Dim objReg As Object
- Dim strTemp As String, lngCount As Long
- Dim objMatchs As Object
- Dim arrResult(1 To 3, 1 To 1) As Variant '返回结果
- Set objReg = CreateObject("VBScript.RegExp")
- objReg.Global = True
-
- '替换掉所有的空格和 标签
- objReg.Pattern = "<.*?>|{.*?}|[\s\t\n]"
- strTemp = objReg.Replace(strVal, "")
- lngCount = Len(strTemp) '字符总数
-
- '汉字字符数量
- objReg.Pattern = "[\u4e00-\u9fa5]"
- Set objMatchs = objReg.Execute(strTemp)
- arrResult(1, 1) = objMatchs.Count
-
- '数字/字母数量
- objReg.Pattern = "[0-9a-zA-Z]"
- Set objMatchs = objReg.Execute(strTemp)
- arrResult(2, 1) = objMatchs.Count
-
- '其他字符数量
- arrResult(3, 1) = lngCount - arrResult(1, 1) - arrResult(2, 1)
-
- CountChar = arrResult
-
- Set objMatchs = Nothing
- Set objReg = Nothing
- End Function
复制代码 |
|