Share via

Excel - Counting cells according to content.

Michael H 0 Reputation points
2026-05-03T17:17:37.2133333+00:00

I have a row of cells, each of which can be blank or contain 1 to 4 different valid characters and possibly any other invalid characters etc. I can count the cells which have only valid characters or only invalid characters, using COUNTIFS. How do I count cells which contain a combination of valid and invalid characters? e.g. Valid - "abcd", "a", "dca", "a b","aaabdcccdd". Invalid - "x", "xyz", "w1123345", $%^&". Mixed valid and invalid (to be counted) - "az", "d123xyz", etc.

Microsoft 365 and Office | Excel | For home | Windows

4 answers

Sort by: Most helpful
  1. IlirU 2,251 Reputation points Volunteer Moderator
    2026-05-04T08:20:51.5466667+00:00

    User's image

    Hi @Michael H,

    Try below formula in cell E4 (see the screenshot above).

    =LET(
    data, A4:D7,
    daypart, {"m";"a";"e";"n"},
    HSTACK(BYROW(data, LAMBDA(a, SUM(--ISBLANK(a)))),
           BYROW(data, COUNTA),
           BYROW(data, LAMBDA(b, SUM(BYCOL(b, LAMBDA(a, --NOT(BYROW(BYCOL(IFNA(REGEXEXTRACT(a, ".", 1) = daypart, TRUE), OR), OR))))))),
           BYROW(data, LAMBDA(b, SUM(IFNA(BYCOL(b, LAMBDA(a, --BYCOL(BYROW(--BYCOL(REGEXEXTRACT(a, ".", 1) = daypart, OR) = {0;1}, OR), AND))), "")))),
           IFERROR(--TRANSPOSE(TEXTSPLIT(TEXTJOIN(";",, BYCOL(I2:L2, LAMBDA(c, ARRAYTOTEXT(BYROW(data, LAMBDA(b, SUM(--BYCOL(IFNA(TEXTSPLIT(TEXTJOIN(";",,
                   IFNA(BYCOL(b, LAMBDA(a, ARRAYTOTEXT(REGEXEXTRACT(a, ".", 1)))), "")), ", ", ";"), "") = LEFT(c, 1), OR)))))))), ", ", ";")), 0))
    )
    
    

    Change the range in formula as per you need.

    HTH

    IlirU

    0 comments No comments

  2. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2026-05-04T03:33:49.8866667+00:00

    Hi,

    In cell G3, enter this formula

    =SUM(1-(1*(BYCOL(A3:D3,LAMBDA(b,LET(a,XMATCH(REGEXEXTRACT(b,".",1),{"m","a","e","n"}),AND(COUNT(a)=LEN(b),LEN(b)>0)))))))

    Hope this helps.

    User's image

    0 comments No comments

  3. EmilyS726 228.2K Reputation points Independent Advisor
    2026-05-03T17:54:14.5066667+00:00

    Hello

    Are the valid and invalid characters limited to a certain set of letters/symbols only? Or is it safe to say, you basically want to count any cell that's not blank? If so, you can use the =COUNTA() function.


  4. Q&A Assist
    2026-05-03T17:17:47.8633333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use COUNTIFS with two complementary criteria on the same range: one to ensure the cell contains at least one valid character, and another to ensure it also contains at least one invalid character.

    Assume:

    • The cells to test are in A1:A100.
    • Valid characters are only a, b, c, d (case-insensitive).
    1. First, define what “invalid” means: any character that is not a–d. Since COUNTIF/COUNTIFS work with patterns, use wildcards to detect presence of valid and invalid characters.
    2. Example approach (requires helper columns because COUNTIFS cannot directly express “not in this set of characters”):
      • In B1, test if the cell contains at least one valid character:
             =OR(ISNUMBER(SEARCH("a",A1)),ISNUMBER(SEARCH("b",A1)),ISNUMBER(SEARCH("c",A1)),ISNUMBER(SEARCH("d",A1)))
        
      • In C1, test if the cell contains at least one invalid character by stripping valid characters and checking what remains. For example, using nested SUBSTITUTE:
             =LEN(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"a",""),"b",""),"c",""),"d",""))>0
        
        This removes all a, b, c, d and checks if any characters are left.
      • Copy B1:C1 down to row 100.
    3. Finally, count cells that have both valid and invalid characters:
         =COUNTIFS(B1:B100,TRUE,C1:C100,TRUE)
      

    This counts cells where:

    • At least one valid character (a–d) is present, and
    • At least one other (invalid) character is also present.

    To count cells that contain only valid characters, use:

    =COUNTIFS(B1:B100,TRUE,C1:C100,FALSE)
    

    To count cells that contain only invalid characters, use:

    =COUNTIFS(B1:B100,FALSE,C1:C100,TRUE)
    

    Adjust the SUBSTITUTE list to match the exact set of valid characters required.


    References:

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.