VideoPhoto

21 April, 2021

COUNTING selectively, in a range

 Excel is primarily for counting and calculations, so let's see couple of examples.

Count UNIQUE values only

Use the following array formula to get the count of unique values in e.g. A1:A100 range:    =SUM(1/COUNTIF(A1:A100,A1:A100)) 
If you don't need the count, but just want to make sure that your list (or range) of values contains unique items only (i.e. none of them is repeated) then you can get the answer with this array formula:
=IF(COUNTA(A1:A100)-SUM(1/COUNTIF(A1:A100,A1:A100))>0,"Not unique","Unique")
These are array formulas, so to enter them, remember, you need to simultaneously hold down the Ctrl and the Shift keys as you press Enter key.

Count ODD or EVEN values only

The SUMPRODUCT function can be utilized for this counting.

To count ODD values only in the A1:A100 range, use this formula: =SUMPRODUCT(--(MOD(A1:$A$100,2)=1),--(A1:$A$100<>""))

To count EVEN values only in the same range, use this formula:
=SUMPRODUCT(--(MOD(A1:$A$100,2)=0),--(A1:$A$100<>""))

The double-negative (--) used in the formulas coerce True and False into 1s and 0s, which are needed (in some cases) to properly interpret them.

SUMPRODUCT is a quite versatile function, working nicely with up to 255(!) array arguments. By default, it multiplies arrays of values, but can be used also for addition, subtraction, and division of arrays; just replace the commas separating arguments with the operators you need (+,-,*). Here I've provided just couple of examples with default multiplication of two arrays.
 

No comments:

Post a Comment

All comments are held for moderation. I reserve the right to edit, censor, delete and - if necessary - block comments.