Search code examples
excelrowoffsetcountif

Excel count with fixed increment


I have a large table of more than 2000 rows, and 5 columns. The values of these cells are either 1 or 0. For each column I want to be able to count the number of 1s in blocks of 20 rows starting from the first row. Example: For each column count the number of 1s in the first 20 cells (A1:A20), then in cells 21 to 41 (A21:A41), then from 42 to 62 and so on. Any help is appreciated.

Many thanks,

egit

I have tried various combination of COUNTIF, ROW and OFFSET but did not work. Spent sometime reading various sites for suggestions but could not find the solution.


Solution

  • =COUNTIF(OFFSET(A1,(ROW()-1)*19,0,20,1),1)

    is used to count the number of 1s in blocks of 20 rows for column A.

    The OFFSET function starts from the cell A1, which is the first cell of the first block of 20 rows.

    Then it uses the ROW() function to determine the current row and subtracts 1 to start counting from the first row.

    Next, it multiplies the current row by 19 to offset the range by 19 rows for each row.

    This is done because the range is starting from A1, which is the first cell of the first block of 20 rows, so it needs to offset by 19 rows to look at the next block of 20 rows.

    The OFFSET function then selects a range of 20 rows starting from that point.

    In the first row, the formula will look at cells A1:A20, in the second row it will look at cells A21:A40 and so on.

    So when you drag the formula down, it will always look at the next block of 20 rows, regardless of where you drag it.

    The COUNTIF function then counts the number of 1s in the selected range and returns the result.