How to Use Excel’s MOD Function to Solve Real-World Problems


The MOD function in Microsoft Excel lets you work out whether one number is divisible by another. If it’s not, it tells you what’s left over. For example, if you divide 3 by 2, the MOD function will return 1. In this guide, I’ll show you how you can use it to solve real-world problems.

The MOD Function Syntax

Before I show you the MOD function in action, let’s take a moment to see how it works:

=MOD(n,d)

where n is the number you want to divide, and d is the divisor. So, if you want to divide 3 by 2, 3 is the number, and 2 is the divisor.

If the divisor is negative, the result will also be negative. On the other hand, if the divisor is 0, Excel will return the #DIV/0! error, since you can’t divide a number by zero.

However, the MOD function is rarely used on its own—its true value really comes to the fore when it’s used alongside other Excel functions and tools.

Example 1: Using the MOD Function to Minimize Leftovers

Let’s say you have various items that you want to package and distribute. Column A contains the product ID, column B tells you how many of each product you have, and row 2 contains the different package sizes. Your aim is to work out the optimal package size for each product, depending on how many of each product you have.

An Excel sheet containing a list of products, their count, and the different package sizes.

In this case, you can use the MOD function to tell you whether the product count in column B is divisible by the package size in row 2, and if it isn’t, how many you have left over.

So, in cell C3, type:

=MOD($B3,C$2)

The dollar symbol ($) placed before a row or column reference—also known as a mixed reference—tells Excel to lock it so that the formula can be applied to other cells in the range.

When you press Enter (or Ctrl+Enter to stay in the same cell), you can see that if you use a package with 10 spaces for product 2805, four will remain unpacked.

The MOD function being used in Excel to calculate the leftover of a product if it is packed in a package with 10 spaces.

Now, when you click and drag the fill handle in cell C3 to the right to complete the row, you can see that using packages with eight or three spaces is ideal for this product.

The fill handle in Excel being used to repeat the MOD function to a row.

Next, in cell G3, you want Excel to tell you the best package size to use for that product. To do this, use INDEX and XMATCH:

=INDEX($C$2:$F$2,1,XMATCH(SMALL(C3:F3,1),C3:F3))

where INDEX($C$2:$F$2,1 tells Excel to return a value from cells C2 to F2, and XMATCH(SMALL(C3:F3,1),C3:F3)) looks up the smallest value in cells C3 to F3 to define the column number that feeds the INDEX part of the formula.

INDEX and XMATCH in Excel being used to return the optional package size for a product.

Related


Don’t Use MATCH in Microsoft Excel: Use XMATCH Instead

Out with old, and in with the new!

Finally, select cells C3 to G3, and double-click the fill handle to duplicate all the formulas to the remaining rows in the range.

Cells C3 to G3 are selected, and the fill handle, which can be double-clicked to duplicate the formula down the range, is highlighted.

Now, thanks to the MOD function being used in cells C3 to F12, column G tells you the best package sizes to use for each product, and you can look in columns C to F for each product to see how many (if any) you will have left over after using this optimal package size.

The MOD, INDEX, and XMATCH functions used in Excel to return the optimal package size for various products.

If the smallest value occurs twice in a row, XMATCH uses the first instance from left to right by default. In this case, this is ideal because you want to use as little packaging as possible.

The beauty of using this combination of functions is that if the product count in column B changes, the optimal package size updates according to the MOD formulas you used in cells C3 to F12. For example, if you originally miscounted product 2805, and you actually have 25 instead of 24, when you update the value in cell B3, you find that the optimal package size changes to five.

The optimal package size for a product is updated in Excel according to an alteration to the product count.

Example 2: Using the MOD Function to Highlight Certain Cells

Excel’s MOD function doesn’t have to be used to return a value—it can also be used to determine which cells should be formatted through conditional formatting.

In this example, imagine you want to create activities for ten groups of people. However, different activities require different numbers of subgroups, so you need to determine which groups can carry out which activity, ensuring all members are involved.

An Excel sheet containing a list of groups and the number of members they have, and a list of activities and their respective subgroup sizes.

First, in cell B1, you want to create a drop-down list of the activities in column D. To do this, with cell B1 selected, head to the “Data” tab on the ribbon, and click the top half of the “Data Validation” button.

The top half of the Data Validation icon in Excel is selected.

Second, in the Allow field of the Data Validation dialog box, choose “List.” Next, place your cursor in the Source field, and select the cells containing the values you want to appear in the drop-down list. Then, click “OK.”

Excel's Data Validation dialog box, with List selected in the Allow field, and cells D2 to D9 selected in the Source field.

Related


How to Add a Drop-Down List to a Cell in Excel

It beats typing in the same options 200 different times manually.

Now, when you select cell B1, you can see a drop-down list of activities, and you’re ready to apply formatting to cells A4 to B13 that tells you whether each group can carry out each activity.

A drop-down list of activities in cell B1 in Excel.

To do this, select cells A4 to B13, and in the Home tab on the ribbon, click Conditional Formatting > New Rule.

Several cells in Excel are selected, and the New Rule button in the Conditional Formatting menu is selected.

Related


I Use Conditional Formatting in Most Spreadsheets: Here’s Why

Conditional formatting is a non-negotiable.

Then, in the New Formatting Rule dialog box, select “Use A Formula To Determine Which Cells To Format,” and in the formula field, type:

=MOD($B4,XLOOKUP($B$1,$D$2:$D$9,$E$2:$E$9))=0

where

  • $B4 is the first of the cells in column B acting as the number to be divided,
  • XLOOKUP($B$1,$D$2:$D$9,$E$2:$E$9) looks up the value in cell B1 and returns the corresponding subgroup size from the lookup table (the divisor), and
  • =0 applies formatting to cells where the number of members in a group is divisible by the number of people required in an activity’s subgroup.

Placing the dollar symbol before the column reference ($B4) in this formula tells Excel that you want the formatting to be conditional on all selected cells in column B.

In other words, you want Excel to format the groups where the whole group size is divisible by the subgroup size required for the activity selected in cell B1.

The MOD function being used in a conditional formatting rule in Microsoft Excel.

Finally, click “Format” to choose the formatting for the cells where these conditions are met. In my case, I’ve gone for a light green cell background.

The Format button in the Conditional Formatting Rule dialog box in Microsoft Excel is selected, and a light green cell background is visible in the preview.

Click “OK” to close the dialog box.

Now, select an activity in the drop-down menu in cell B1, and the groups that can participate in that activity will be highlighted according to the formatting you selected. In this example, you can see that groups 1, 2, 3, 5, and 8 can take part in the quiz because the total number of members in those groups is divisible by the subgroup size required for this activity.

An Excel sheet that uses MOD, XLOOKUP, and conditional formatting to color the groups that can take part in the quiz.

On the other hand, when you select “Hiking” from the list, only group 5 is identified as being able to do this activity, as it’s the only group whose overall number is divisible by the subgroup size.

An Excel sheet that uses MOD, XLOOKUP, and conditional formatting to color the group that can take part in hiking.

Example 3: Using the MOD Function to Spit Date-Time Values

Microsoft Excel’s MOD function can be used to extract the decimal part of a number. In this example, typing:

=MOD(A2,1)

into cell B2 divides 10.2 by 1, and tells you that the remainder of this division is 0.2.

The MOD function in Excel being used to extract the decimal from a number.

You can also extract the integer from this number using the INT function, which rounds a value down to the nearest whole number. So, typing:

=INT(A2)

into cell C2 removes the decimal from the value in cell A2, returning the whole number only.

The INT function being used in Excel to extract an integer from a decimal.

In the same way, you can use MOD and INT to split date-time values.

Here, cell A2 contains a date and time, and cell A3 contains the corresponding serial number.

A date-time value in cell A2 in Excel, and the corresponding serial number in cell A3.

Excel stores dates and times as serial numbers, which can be seen when you change the number format to “General.” So, in this example, the values of cells A2 and A3 are identical, but the number format for cell A2 is a custom date-time number format, while the number format for cell A3 is the general number format.

In the serial number in cell A3, 45782 represents the date, and 0.4375 represents the time, and you can use MOD and INT to split these up.

First, select cell B2, and change the number format to “Time” in the Number group of the Home tab on the ribbon.

A cell in Excel is selected, and the Time number format is selected in the Number group.

Related


Excel’s 12 Number Format Options and How They Affect Your Data

Adjust your cells’ number formats to match their data type.

Then, select cell C2, and change the number format to “Short Date.”

A cell in Excel is selected, and the Short Date number format is selected in the Number group.

Then, go back to cell B2, and type:

=MOD(A2,1)

This extracts the decimal (time) from the serial number representation of the date-time value in cell A2, and because you set the cell number format to Time, it converts the serial decimal to a time value.

The MOD function in Excel being used to extract the time from a date-time value.

Finally, go back to cell C2, and type:

=INT(A2)

This extracts the integer (date) from the serial number in cell A2 by rounding it down to the nearest whole number, and since you set this as a short date number format, it converts the serial integer to a date value.

The INT function being used in Excel to extract a date from a date-time value.


Half the battle with using functions in Microsoft Excel is knowing which one is best for solving your problem. For example, if you want to refine your calculations, the AGGREGATE function will probably do the trick, and if you want to create a list of numbers, the SEQUENCE function is your best bet. Luckily, Excel is here to help you find the function you need—you can click the “fx” button next to the formula bar or search for a function in the Insert Function dialog box, and Excel’s function tools will help you along the way as you craft the perfect formula.

By admin

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *