dextervip
dextervip

Reputation: 5069

How to count cell numbers based on a set of numbers

I am looking for a function which counts how many numbers in a range of cells are in the set of numbers

For example I have the set of numbers(1,2,3) and my cells contains 1 | 2 | 3 | 4 | 5 | 3 , the count should return 4

I have tried using countif but no success, I would like to have an excel function Ex.: =countif(A1:D5,...)

Upvotes: 0

Views: 3074

Answers (2)

Alex P
Alex P

Reputation: 12487

How about this? Assume data is in range A1:D5 and you want to count cells with a value of 1, 2 or 3:

=SUM(COUNTIF(A1:D5, {"1","2","3"}))

Upvotes: 3

ZarakiKenpachi
ZarakiKenpachi

Reputation: 457

I hope my pseudo-code would be understandable

int count(int *set, int set_size, int *cells, int cells_size)
{
    int v = 0;

    // For every number in set
    for(int i = 0; i < set_size; ++i)
    {
        // Loop through every number in cells
        for(int j = 0; j < cells_size; ++j)
        {
            // If number in cells equals number in set, increment v
            if(cells[j] == set[i])
            {
                v++;
            }
        }
    }

    // Result is in v, return it
    return v;
}

Of course you can optimize a bit with using better containers than just arrays and sizes of them, but I hope you get the basics from this.

Note I used C-like language for pseudo-code, if anything is unclear I can explain further.

Upvotes: 0

Related Questions