Count specific characters in a cell - Excel

后端 未结 4 1577
日久生厌
日久生厌 2021-02-11 03:43

I would like to count all special characters (!%_*?+-,) in a cell.

For example:

With this formula =LEN(D2)-LEN(SUBSTITUTE(D2;\"!\";\"\")) i

相关标签:
4条回答
  • 2021-02-11 04:29

    You can do this with a simple array formula:

    =SUM(LEN(A2) - LEN(SUBSTITUTE(A2,Special_Characters,"")))
    

    Special_Characters is a range listing all your special characters. You could manually enter them as an array constant if you prefer:

    =SUM(LEN(A2) - LEN(SUBSTITUTE(A2,{"%";"_";"*";"?";"+";"-";",";"!"},"")))   
    

    but the named range seems simpler.


    To array-enter a formula, after entering the formula into the cell or formula bar, hold down ctrl + shift while hitting enter. If you did this correctly, Excel will place braces {...} around the formula.

    If you prefer a VBA solution, I would suggest the code below. You will need to modify .Pattern to include any other characters you do NOT want to count. In the code below, any character that is not an upper or lower case letter, or a digit, will be counted as a special character.

    Option Explicit
    Function SpecialChars(S As String) As Long
        Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    With RE
        .Global = True
        .Pattern = "[^A-Za-z0-9]"
        SpecialChars = Len(S) - Len(.Replace(S, ""))
    End With
    End Function
    
    0 讨论(0)
  • 2021-02-11 04:35

    =8*LEN(D2)-LEN(SUBSTITUTE(D2;"!";""))-LEN(SUBSTITUTE(D2;"%";""))-LEN(SUBSTITUTE(D2;"_";""))-LEN(SUBSTITUTE(D2;"*";"")) -LEN(SUBSTITUTE(D2;"?";""))-LEN(SUBSTITUTE(D2;"+";""))-LEN(SUBSTITUTE(D2;"-";""))-LEN(SUBSTITUTE(D2;",";""))

    0 讨论(0)
  • 2021-02-11 04:37

    Using formulas only, not VBA, this is possible with the following two approaches:

    Consider the text in A1:

    First approach:

    Multiple nested SUBSTITUTE:

    =LEN(A1)-LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,",",""),"-",""),"+",""),"?",""),"*",""),"_",""),"%",""),"!",""))
    

    Second approach:

    Using SUMPRDUKT for getting the MID function in array context to get each single charachter of the string in A1 compared with each single special character:

    =SUMPRODUCT(--(MID(A1,ROW($1:$1000),1)={"!","%","_","*","?","+","-",","}))
    
    0 讨论(0)
  • 2021-02-11 04:38

    Here is a simple version I created:

    Function CountSpecialCharacters(rng As Range) As String
        Dim regEx As New RegExp, matches As MatchCollection
    
        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = "[^a-zA-Z0-9]" '~~~> this counts any character not a to z or a number
        End With
    
        Set matches = regEx.Execute(rng)
    
        CountSpecialCharacters = matches.Count
    End Function
    

    Two points:

    1. In the VBA editor you need to set References > Microsoft VBScript Regular Expressions 5.5
    2. You call the function on your spreadsheet e.g. =CountSpecialCharacters(A2)
    0 讨论(0)
提交回复
热议问题