简体   繁体   中英

Excel formula for count cells if not null, invalid or 0

I have a formula that I want to use to check if a cell does not have an 'invalid' value in it. However, it is also counting empty cells, and cells that have anything in it that isn't equal to zero:

=COUNTIF(A2:A200,"<>0")

This only checks if the cell does not have a value of "0". What can I add to it so that it will not count empty cells, or cells with values like:

#######
VALUE?
r

etc. All I want is to count how many cells have a number in them that does not equal 0, or an error.

The array formula below first counts all non-0 and non-null values and then subtracts the count of cells that contain errors.

You need to press CTRL + SHIFT + ENTER to properly execute this formula:

=COUNTIFS(A2:A200,"<>0", A2:A200,"<>"&"", A2:A200,"<>"&"NIL") - SUM(IF(ISERROR(A2:A200),1,"")) - SUM(IF(ISNA(A2:A200),1,""))

This formula will count only numbers <> 0, excluding blanks, error messages, etc. BUT it will not exclude the cells that display ###### (but really contain a number) if the reason for that is a column that is too narrow, or a negative date or time value.

 =SUMPRODUCT(--ISNUMBER(A2:A200))-COUNTIF(A2:A200,0)

If you really want to avoid counting cells that display ####### when the underlying contents is a number not equal to zero, you will need to use a UDF to act on the Text property of the cell. In addition, narrowing or widening the column to produce that affect will not trigger a calculation event that would update the formula, so you need to somehow do that in order to ensure the formula results are correct.

That is why I added Application.Volatile to the code, but it is still possible to produce a situation where the result of the formula does not agree with the display in the range being checked, at least until the next calculation event takes place.

To enter this User Defined Function (UDF), alt-F11 opens the Visual Basic Editor. Ensure your project is highlighted in the Project Explorer window. Then, from the top menu, select Insert/Module and paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

 =CountNumbersNEZero(A2:A200)

in some cell.

Option Explicit
Function CountNumbersNEZero(rg As Range) As Long
Application.Volatile
    Dim C As Range
    Dim L As Double
For Each C In rg
    If IsNumeric(C.Text) Then
        If C.Text <> 0 Then L = L + 1
    End If
Next C
CountNumbersNEZero = L
End Function

You can nest up to 7 valid or invalid entries. If you need to have more than that you should perhaps designate a column for your "black list" of entries that you can add to when an occurrence causes a count that you don't believe should be valid. For example:

=IF(ISERR(VLOOKUP(A1,Sheet1!E:E,1,FALSE))=FALSE,1,0)

Where column "E" is your list of values that are considered invalid. Drag this down next to your criteria and sum.

Edit: I wasn't aware of countifs. So you have a couple of solutions, here, depending on your preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM