如何计算Excel中特定类型的字符

时间:2013-11-10 13:14:57

标签: excel

我想计算一个单元格中出现的小写字符数,然后出现多少个大写字符以及出现多少个特殊字符。我怎样才能实现这样的目标呢?

2 个答案:

答案 0 :(得分:2)

考虑以下四个用户定义函数:

Public Function LowC(r As Range) As Long
    Dim s As String, ch As String
    Dim IAmTheCount As Long, L As Long
    s = r.Text
    IAmTheCount = 0
    For L = 1 To Len(s)
        If Mid(s, L, 1) Like "[a-z]" Then
            IAmTheCount = IAmTheCount + 1
        End If
    Next L
    LowC = IAmTheCount
End Function

Public Function HighC(r As Range) As Long
    Dim s As String, ch As String
    Dim IAmTheCount As Long, L As Long
    s = r.Text
    IAmTheCount = 0
    For L = 1 To Len(s)
        If Mid(s, L, 1) Like "[A-Z]" Then
            IAmTheCount = IAmTheCount + 1
        End If
    Next L
    HighC = IAmTheCount
End Function

Public Function NumC(r As Range) As Long
    Dim s As String, ch As String
    Dim IAmTheCount As Long, L As Long
    s = r.Text
    IAmTheCount = 0
    For L = 1 To Len(s)
        If Mid(s, L, 1) Like "[0-9]" Then
            IAmTheCount = IAmTheCount + 1
        End If
    Next L
    NumC = IAmTheCount
End Function

Public Function OtherC(r As Range) As Long
    OtherC = Len(r.Text) - LowC(r) - HighC(r) - NumC(r)
End Function

他们将在单个单元格中返回大写,小写,数字和其他字符的数量。

答案 1 :(得分:0)

使用Excel功能是可能的,虽然VBA对我来说似乎更简单。

使用Excel函数:

大写字母数:

=SUMPRODUCT((MID(A1,ROW(INDIRECT("1:99")),1)>="A")*(MID(A1,ROW(INDIRECT("1:99")),1)<="Z")*EXACT(MID(A1,ROW(INDIRECT("1:99")),1),UPPER(MID(A1,ROW(INDIRECT("1:99")),1))))

小案数

SUMPRODUCT((MID(A1,ROW(INDIRECT("1:99")),1)>="A")*(MID(A1,ROW(INDIRECT("1:99")),1)<="Z")*EXACT(MID(A1,ROW(INDIRECT("1:99")),1),LOWER(MID(A1,ROW(INDIRECT("1:99")),1))))

数字计数

=SUMPRODUCT(--ISNUMBER(-MID(A1,ROW(INDIRECT("1:99")),1)))

特价计数

=LEN(A1)-SUM(SUMPRODUCT((MID(A1,ROW(INDIRECT("1:99")),1)>="A")*(MID(A1,ROW(INDIRECT("1:99")),1)<="Z")*EXACT(MID(A1,ROW(INDIRECT("1:99")),1),UPPER(MID(A1,ROW(INDIRECT("1:99")),1)))),SUMPRODUCT((MID(A1,ROW(INDIRECT("1:99")),1)>="A")*(MID(A1,ROW(INDIRECT("1:99")),1)<="Z")*EXACT(MID(A1,ROW(INDIRECT("1:99")),1),LOWER(MID(A1,ROW(INDIRECT("1:99")),1)))),SUMPRODUCT(--ISNUMBER(-MID(A1,ROW(INDIRECT("1:99")),1))))