Count cells with values in even rows
This tutorial shows how to count cells with values in even rows through the use of an Excel formula or VBA
Example: Count cells with values in even rows
=SUMPRODUCT(--(MOD(ROW(B5:B11),2)=0),--(B5:B11<>""))
|
This formula uses the Excel ROW and MOD functions to return the remainder of 0 for a cell in an even row and 1 for a cell in an odd row. The Excel SUMPRODUCT function then performs this calculation for every cell in the specified range (B5:B11) that contains a value and returns the number of occurrences, which includes two even values (5 and 24 from cells B8 and B10, respectively), and sums these occurrences. Therefore, the formula will return a value of two given there are two cells in even rows, within the selected range, that contain values.
|
Sub Count_cells_with_values_in_even_rows()
'declare a variable
Dim ws As Worksheet
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'count cells with values in even rows
ws.Range("D5").Formula = "=SUMPRODUCT(--(MOD(ROW(B5:B11),2)=0),--(B5:B11<>""))"
ws.Range("D5").Formula = "=SUMPRODUCT(--(MOD(ROW(B5:B11),2)=0),--(B5:B11<>""))"
End Sub
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("D5") in the VBA code.
Data Range: Select the values that you want to test and count by changing the range ("B5:B11") to any range in the worksheet, that doesn't conflict with the formula.
Worksheet Selection: Select the worksheet in which you want to count the cells with values in even rows by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
Output Range: Select the output range by changing the cell reference ("D5") in the VBA code.
Data Range: Select the values that you want to test and count by changing the range ("B5:B11") to any range in the worksheet, that doesn't conflict with the formula.
Worksheet Selection: Select the worksheet in which you want to count the cells with values in even rows by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
EXPLANATION
This tutorial shows how to count cells with values in even rows only from a selected range through the use of an Excel formula or VBA.
This tutorial provides one Excel method that can be applied to count the number of cells with values in even rows by using the Excel SUMPRODUCT, MOD and ROW functions.
This tutorial provides one VBA method that can be applied to count the number of cells with values in even rows from a selected range using the VBA formula and the exact same formula that is provided in the Excel method.
FORMULA
=SUMPRODUCT(--(MOD(ROW(range),2)=0),--(range<>""))
=SUMPRODUCT(--(MOD(ROW(range),2)=0),--(range<>""))
ARGUMENTS
range: A range of values that you want to test if there are cells in even rows that contain values and count the number of these occurrences.
range: A range of values that you want to test if there are cells in even rows that contain values and count the number of these occurrences.
RELATED TOPICS
Related Topic | Description | Related Topic and Description |
---|---|---|
Count cells with values in odd rows | How to count cells with values in odd rows using Excel and VBA methods | |
Count cells with values in odd columns | How to count cells with values in odd columns using Excel and VBA methods | |
Count cells with values in even columns | How to count cells with values in even columns using Excel and VBA methods | |
Count cells with specific value in odd rows | How to count cells with specific value in odd rows using Excel and VBA methods | |
Count cells with specific value in even rows | How to count cells with specific value in even rows using Excel and VBA methods |
RELATED FUNCTIONS
Related Functions | Description | Related Functions and Description |
---|---|---|
SUMPRODUCT Function | The Excel SUMPRODUCT function multiplies corresponding ranges and returns the sum of these values | |
MOD Function | The Excel MOD function returns the remainder after dividing a number by the divisor | |
ROW Function | The Excel ROW function returns the first row number of the selected reference |