Excel:计算一列数据中的文本值是仅Alpha,字母数字和所有类型

时间:2010-12-14 04:16:51

标签: excel

假设我有一个包含10,000个条目的列。我想要计算这些条目中有多少只包含字母,数字和字母,最后还有其他所有内容。请注意,所有数据都存储为文本,而不是数字。

1 个答案:

答案 0 :(得分:2)

我测试了以下内容,似乎有效。

注意要使其工作,您需要在VB编辑器中添加对 Microsoft VBScript Regular Expressions 5.5 的引用:

工具>参考文献> Microsoft VBScript正则表达式5.5

Sub CountTextPatterns()
Dim rngToCheck As Range
Dim cl As Range

Set rngToCheck = Range("A1:A10000") //Set up the range that contains the text data

Dim numbersLetters As VBScript_RegExp_55.RegExp
Set numbersLetters = New RegExp
numbersLetters.Pattern = "^\d*[a-zA-Z][a-zA-Z0-9]*$" //Regex pattern to match alphanumeric with at least one letter

Dim lettersOnly As VBScript_RegExp_55.RegExp
Set lettersOnly = New RegExp
lettersOnly.Pattern = "^[a-zA-Z]+$" //Regex pattern for letters only

Dim cntNumbersLetters As Long, cntLetters As Long, cntRemainder As Long
cntNumbersLetters = cntLetters = cntRemainder = 0

//Loop through range, match cell contents to pattern, and increment count accordingly
For Each cl In rngToCheck
    If lettersOnly.Test(cl) Then
        cntLetters = cntLetters + 1
    ElseIf numbersLetters.Test(cl) Then
        cntNumbersLetters = cntNumbersLetters + 1
    End If
Next

//For anything that isn't letters only or numbers and letters, simply subtract from counts and total row count
cntRemainder = rngToCheck.Rows.Count - cntNumbersLetters - cntLetters

Debug.Print "Numbers and Letters = " & cntNumbersLetters & vbTab & "Letters = " & cntLetters & vbTab & "Remainder = " & cntRemainder

End Sub