ablebits.com/office-addins-blog/2017/09/13/excel-mod-function-remainder/
The Excel MOD function is designed to get the division remainder. Simple, isn't it? But
this function is capable of much more than its original purpose, and in a moment you will
learn a few non-trivial uses of MOD in Excel that may prove helpful in your work.
MOD(number, divisor)
Where:
SUMPRODUCT((MOD(ROW(range),2)=0)*(range))
SUMPRODUCT((MOD(ROW(range),2)=1)*(range))
Assuming B2:B7 are the cells to sum, the formulas look as follows:
In these formulas, the MOD ROW combination determines which rows to sum and
SUMPRODUCT adds up the values. Here's how:
ROW supplies an array of row numbers to the number argument of the MOD function.
MOD divides each row number by 2, and you check the result:
To sum even rows, the result should be 0 because even numbers are divisible by 2
evenly, without remainder.
To sum odd rows, the result should be 1 because odd numbers divided by 2 leave
2/9
a remainder of 1.
Instead of SUMPRODUCT, you could use the IF function to evaluate the condition and
SUM to add up the numbers (it's an array formula, which is entered by pressing Ctrl +
Shift + Enter):
=SUM(IF(MOD(ROW($B$2:$B$7),2)=0,$B$2:$B$7,0))
Personally, I'd stick with SUMPRODUCT because ROW is a volatile function and using it
in an array formula makes the whole formula volatile. That means the formula would
recalculate with every change you make to a workbook, which may noticeably slow down
your Excel.
SUMPRODUCT((MOD(ROW(range)-ROW(first_cell)+1,n)=0)*(range))
For example, to sum every 3rd cell in the range C2:C10, the formula goes as follows:
=SUMPRODUCT((MOD(ROW($C$2:$C$10)-ROW($C$2)+1,3)=0)*($C$2:$C$10))
Compared to the previous example, this formula has a bit more complex "filtering logic":
MOD(ROW($C$2:$C$10)-ROW($C$2)+1,3)=0
First, you subtract the first row from all other rows in the range to get an array of relative
row numbers {0;1;2;3;4;5;6;7;8}, to which you add 1 to start counting from one:
{1;2;3;4;5;6;7;8;9}
The above array goes into the number argument of the MOD function, which returns the
remainder after dividing each number by 3: {1;2;0;1;2;0;1;2;0}
Note that zeros appear for every 3rd row, and MOD()=0 tells Excel to return TRUE only
for these rows, FALSE for all other rows. With TRUE equating to 1 and FALSE to 0, we
are left with an array of {0,0,1,0,0,1,0,0,1}
3/9
Finally, SUMPRODUCT multiplies the above array and C2:C10, and then adds up the
products. Since multiplying by zero always gives 0, only the rows that contain 1 in the
first array survive the multiplication.
SUMPRODUCT((MOD(COLUMN(range)-COLUMN(first_cell)+1,n)=0)*(range))
For example, to add up numbers in every 4th column in the range B2:I2, you go with this
formula:
=SUMPRODUCT((MOD(COLUMN($B2:$I2)-COLUMN($B2)+1,4)=0)*($B2:$I2))
In this example, we have order details in three different cells in column B, so we are
looking to concatenate each block of three cells, beginning with B2. This can be done
with the following formula:
Important note! For the formula to work correctly, it shall be entered in the 3rd row with
data (C4 in this example), and then copied down the column.
4/9
The formula's logic is very simple:
First, you check if a given row is divisible by 3 with no remainder: MOD(ROW()-1, 3)=0.
To get the relative position of the current row, we subtract 1 from the actual row number
(since our data begins in row 2, not in row 1). If the division remainder is equal to 0, the
CONCATENATE function combines values from 3 cells and separates them with the
delimiter of your choosing ("-" in this example). The use of relative cell references
ensures that three different cells are concatenated each time.
Tip. Instead of the -1 correction, you can get the relative position of each row as we did
in the above example, i.e. IF(MOD(ROW()-ROW($B$2)+1, 3)=0, CONCATENATE(), "")
This will make your formula more flexible and it won't require further adjustments no
matter in which row your data starts.
SUMPRODUCT((MOD(range,2)=1)*1)
SUMPRODUCT((MOD(range,2)=0)*1)
With numbers in cells A2 to A8, the formulas take the following shape:
=SUMPRODUCT((MOD(A2:A8,2)=1)*1)
=SUMPRODUCT((MOD(A2:A8,2)=0)*1)
5/9
How to use MOD in Excel to highlight cells
Aside from calculating cells based on a specific remainder, the MOD function in Excel
can be used to highlight those cells. For this, you will have to create a formula-based
conditional formatting rule. The detailed steps to set up a rule can be found here, and
the formulas are provided below.
=MOD(A2,2)=1
=MOD(A2,2)=0
6/9
To identify integers, just follow this logical chain: any number divided by 1 equals itself,
meaning an integer divided by 1 is always the same integer, meaning the remainder is
zero, meaning we can use this simple MOD formula:
=MOD(A2,1)=0
If the remainder is greater than zero, you are dealing with a fraction (including
decimals):
=MOD(A2,1)>0
Since in formula-based conditional formatting rules cell references are relative to the top
left cell of the selected range, we reference A2 in this example:
For example, to highlight cells containing 10 and all its multiples, use this formula:
=MOD(A3,10)=0
Where A3 is the top left cell in the range for which you create the rule (A3:C9 in the
screenshot below).
7/9
Or, you can input the number of interest in any empty cell, say C1, and reference that cell
in your formula (remember to lock the cell with an absolute reference):
=MOD(A3, $C$1)=0
To make a data validation rule based on a formula, click Data > Data Validation. On the
Settings tab, select Custom in the Allow box, and enter your formula in the Formula box.
For the detailed steps, please see How to create a custom validation rule in Excel.
For example, to allow only integers in cells A2:A8, select these cells, and configure a rule
with this formula:
=MOD(A2,1)=0
8/9
In a similar fashion, you can limit the data entry to even or odd numbers:
=MOD(A2,2)=1
=MOD(A2,2)=0
That's how you use the MOD function in Excel to find the division remainder and perform
other calculations. To have a closer look at the formulas discussed in this tutorial, you
are welcome to download our sample Excel MOD workbook. For more formula examples,
please check out the resources at the end of this page.
I thank you for reading and hope to see you on our blog next week!
9/9