{"id":15054,"date":"2024-02-02T14:52:51","date_gmt":"2024-02-02T14:52:51","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=15054"},"modified":"2024-02-02T14:52:51","modified_gmt":"2024-02-02T14:52:51","slug":"mod-of-big-numbers-in-excel","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2024\/02\/mod-of-big-numbers-in-excel\/","title":{"rendered":"Calculate MOD of Large Numbers in Excel"},"content":{"rendered":"
[et_pb_section fb_built=”1″ admin_label=”section” _builder_version=”4.17.6″ custom_padding=”0px|0px|0px|0px|false|false” da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row admin_label=”row” _builder_version=”4.16″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ _module_preset=”default” global_colors_info=”{}”]<\/p>\n
This article is all about MOD function in Excel. Every aspect of the MOD function is covered here in this article. At the end of this article, I have also explained a VBA code to calculate the MOD of large numbers.
\nI have also explained the shortcomings of the MOD formula in Excel and how to overcome those shortcomings.<\/p>\n
Let’s start with the basics – What is the MOD function?<\/p>\n
MOD is a short form of Modulus. Modulus is a function that returns the remainder when you divide a number with another number. Using simple Excel Formula, you can easily calculate the MOD of any 2 numbers.<\/p>\n MOD Function<\/p><\/div>\n As shown in the above picture, you can see there are two input parameters required for this Function. Number and Divisor.<\/em><\/p>\n Using the above formula, you can calculate MOD of a maximum of 12 digits. If number goes beyond 12 digits, then you get #NUM!<\/strong> error as shown below:<\/em><\/p>\n Mod Error for Big Numbers<\/p><\/div>\n Yesterday, In office, one of my colleague (Bas Vermeulen) was trying to calculate the IBAN Numbers<\/a> for a Given BANK Name, Country Code and Bank Account Number. In the process of calculating IBAN Number, he needed to calculate MOD-97<\/em> <\/strong>of really bog number (approx 22 digit number).<\/p>\n Therefore, to help him, I created a VBA function that can calculate the MOD of any number of digits<\/p>\n To overcome the MOD function error, I came up with the following logic, so that no matter how big the number is, MOD can still be calculated. Therefore, I went back to the basics of Division, which I had studied in my school days – How to divide a big number.<\/p>\n So here is what you do.<\/p>\n Step 1: <\/strong>First pick up the first n length of digit from the Large Number provided (Where n = Length of Divisor + 1) – Just to make sure that the big number is divided by a small number. Refer to the image below for more clarity<\/p>\n Find MOD Manually – Divide Method<\/p><\/div>\n<\/div>\n With the above process, it is clear that, if we automate the above step in VBA then, there you can calculate the MOD of any big number. Because at a time, you are dividing a subset of the whole number. The number of iterations will increase based on how big the number is – but it will be able to calculate.<\/p>\n There is one shortcoming in the above code – in case when Divisor is more than 5 digits – it will give an error<\/strong><\/em> The only, issue with the below function is that it accepts Divisor as a string and also returns the result as a string.<\/p>\n So, now you have a solution to calculate Mod for a big Number and Big Divisor. [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" Excel Trick to Calculate MOD of Big Numbers. This article is all about MOD function in Excel. Every aspect of the MOD function is covered here in this article. At the end of this article, I have also explained a VBA code to calculate the MOD of large numbers. I have also explained the shortcomings […]<\/p>\n","protected":false},"author":45,"featured_media":242618,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1672,1673,1246,1676],"tags":[],"class_list":["post-15054","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-formula","category-excel-functions","category-macro","category-excel-tips"],"yoast_head":"\n
\nFor Example:<\/strong> 19 MOD 5 = 4 ( Explanation : If you divide 19 with 5 then remainder will be 4 (5*3 = 15 and 19-15 = 4)<\/p>\nHow to calculate MOD using Excel Formula<\/h2>\n
<\/a>
Important Note:<\/h3>\n
<\/a>
Need of Calculating MOD of Big Numbers<\/h2>\n
VBA Code to Calculate MOD of Big Numbers<\/h2>\n
\nStep 2: <\/strong>Now for this subset of the digit, it is easy to calculate the MOD and get the Remainder
\nStep 3: <\/strong>Now, simply append this remainder in the remaining digit of the larger number
\nStep 4: <\/strong>Now using this as a new Number repeat the steps from Step 1…<\/p>\n\nFunction MODForBigNum(bigNum As String, modVal As Integer) As Integer\n \n Dim currentLenNum As Integer\n Dim partitionCount As Integer\n Dim remainingString As String\n \n currentLenNum = VBA.Len(bigNum)\n partitionCount = VBA.Len(modVal) + 1\n \n remainingString = bigNum\n \n While currentLenNum > partitionCount\n stringToInt = VBA.Left(remainingString, partitionCount)\n remainingString = VBA.Right(remainingString, currentLenNum - partitionCount)\n remainder = stringToInt - (Int(stringToInt \/ modVal) * modVal)\n remainingString = remainder & remainingString\n currentLenNum = VBA.Len(remainingString)\n Wend\n \n MODForBigNum = remainingString - (Int(remainingString \/ modVal) * modVal)\n \nEnd Function\n<\/code><\/pre>\n
\nTo overcome the above problem, I did some changes to the above code and now it should be working any number of Number and Divisor<\/em> <\/strong><\/p>\n\nFunction MODForBigNum2(bigNum As String, modVal As String) As String\n \n Dim currentLenNum As Integer\n Dim partitionCount As Integer\n Dim remainingString As String\n \n currentLenNum = VBA.Len(bigNum)\n partitionCount = VBA.Len(modVal) + 1\n \n remainingString = bigNum\n \n While currentLenNum > partitionCount\n stringToInt = VBA.Left(remainingString, partitionCount)\n remainingString = VBA.Right(remainingString, currentLenNum - partitionCount)\n remainder = stringToInt - (Int(stringToInt \/ modVal) * modVal)\n remainingString = remainder & remainingString\n currentLenNum = VBA.Len(remainingString)\n Wend\n \n MODForBigNum2 = remainingString - (Int(remainingString \/ modVal) * modVal)\n \nEnd Function\n<\/code><\/pre>\n
Calculate Mod-97 of Large Numbers in Excel<\/h3>\n
\nIn my next article, I will Publish a Tool to calculate IBAN Numbers automatically.[\/et_pb_text][et_pb_blurb title=”Suggested Articles:” use_icon=”on” font_icon=”||fa||900″ _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ hover_enabled=”0″ icon_font_size=”2em” saved_tabs=”all” global_colors_info=”%91%93″ custom_css_blurb_image_phone=”position:absolute;||margin-bottom: 0px;||border: 0.5em solid #30657f !important;||box-shadow: 0 0 0 0.2em #fff;||border-radius:50%;||background: #30657f;||transform: translateX(-3.7em);” sticky_enabled=”0″]<\/p>\n\n