{"id":15002,"date":"2018-09-25T06:34:28","date_gmt":"2018-09-25T06:34:28","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=15002"},"modified":"2022-08-17T19:14:42","modified_gmt":"2022-08-17T19:14:42","slug":"vba-to-spell-numbers-to-letters","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2018\/09\/vba-to-spell-numbers-to-letters\/","title":{"rendered":"UDF to Convert Numbers to Letters"},"content":{"rendered":"
[et_pb_section fb_built=”1″ admin_label=”section” _builder_version=”4.16″ 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=”{}”]First of all, wouldn’t it be great if we have a built-in function in Microsoft Excel to Convert Numbers to Letters? <\/strong> It means, simply, by using a formula in excel, I could Spell Numbers in Words. As we do not have any built in function as such in Microsoft Excel yet – therefore, I have created this User Defined function (UDF<\/a>) to convert Numbers to Letters. You can also download a FREE Excel Workbook with this UDF <\/a>in it. <\/em> Though there was a VBA code available Microsoft office wiki <\/a> about spelling currency in words. I, too had written an article previously about converting Indian currency in words. <\/a><\/p>\n For example: if you want the maximum scale unit to be used to spell your amount is Billion<\/em> (like Thousand, Million, and Billion) and you provide an amount which is (more than 999 Billion) 1000 Billion or more. Then, in this case, these functions were not capable of dealing with that and returns the strange result.<\/p>\n To overcome above limitation,<\/strong> I have created a function in VBA using recursive method<\/strong> to convert Numbers to letters<\/strong>. That means in the above example, this function will be able to give you correct spelling for 1000 Billion or even more. This is possible because of usingrecursive function <\/strong>. In simple language – if a function calls itself from its own body the function is called recursive function and this method is called recursion.<\/em><\/p>\n Since this is User Defined Function to convert numbers to words <\/strong> or in other words<\/a>, spell numbers in Words. <\/strong> – You can use it as a formula in any cell in your excel workbook. <\/em><\/p>\n If you do not know what is UDF – User Defined Function – Refer this article.<\/a><\/em><\/p>\n To make it re-usable and simplify my previous code to convert numbers to letters<\/a>, I have made a logical split <\/a>of the whole code. I have made it in a more modular way which will be a lot easier to understand and change according to your need.<\/p>\n 1. Below is the VBA function [recursive function] to spell numbers to letters. It does not care about the currency Unit or sub-Unit etc. This is simply used to convert your number in words<\/strong>. The benefit of creating this as a separate function is that you can use this function to convert any type of Numbers to Letters. It is not necessary that you should use this for converting a currency amount to words only. Important to note that the above code is for the Indian continent Number Scale System Only. You can customize it according to your need.<\/p>\n Example: In Indian Continent Number Scale System: 6734862: <\/strong> Sixty Seven Lakhs Thirty-Four Thousand Eight Hundred and Sixty Two.<\/p>\n I have customized the above function for American Number Scale system <\/strong>. Following is the code for the American Number Scale System:<\/p>\n Example: In American Number Scale System: 6734862: <\/strong> Six Million Seven Hundred Thirty-Four Thousand Eight Hundred and Sixty Two.<\/p>\n For above function to work correctly, DO NOT FORGET to copy the generic functions below.<\/a> Spell Numbers to Letters functions uses these generic functions.<\/p>\n<\/div>\n 1.<\/strong> Round-Up the input number provided to Spell the currency up to two-digit. Note:<\/strong> Likewise you can use this method for any other types of Numbers like Weight, Height, or any kind of Units and Sub-Units.<\/p>\n<\/div>\n In American Number Scale System : 6734862.456 : <\/strong> Six Million Seven Hundred Thirty Four Thousand Eight Hundred and Sixty Two Dollars<\/strong> and Fourty Six Cents.<\/strong><\/p>\n Basically, this function works on same principal as above. The only change from above function is that – you should call corresponding function for Indian Number Scale system.<\/p>\n In Indian Continent Number Scale System : 6734862.456 : <\/strong> Sixty Seven Lakhs Thirty Four Thousand Eight Hundred Sixty Two Rupees<\/strong> and Fourty Six Paise.<\/strong><\/p>\n Following are the generic functions. We use these functions to translate single digits, tens and hundreds in corresponding numbers system. <\/a>Follow the following 5 simple steps to use this UDF<\/a>. Finally, you can download your FREE Excel workbook with UDF to convert numbers to word. Moreover, You can also look into the code and customize it to fit your need. [\/et_pb_cta][et_pb_blurb title=”Suggested Artcles:” use_icon=”on” font_icon=”||fa||900″ _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ hover_enabled=”0″ global_colors_info=”{}” sticky_enabled=”0″]<\/p>\n [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" First of all, wouldn’t it be great if we have a built-in function in Microsoft Excel to Convert Numbers to Letters? It means, simply, by using a formula in excel, I could Spell Numbers in Words. In banking or accountancy, this is used to write grand total amounts. Amounts are mentioned in Numbers as well […]<\/p>\n","protected":false},"author":45,"featured_media":242467,"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":[1673,1246,1682,2056],"tags":[],"class_list":["post-15002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-functions","category-macro","category-popular-articles","category-udf"],"yoast_head":"\n
\nIn banking or accountancy, this is used to write grand total amounts. Amounts are mentioned in Numbers as well as words. You probably have seen it on invoices or on Bank Cheques.<\/p>\n
\nWe can write the above number in words in many ways in different languages, countries Number scales, etc. Here are the two, for which, I have adjusted the function.
\nIn American English: <\/strong> Six Million Seven Hundred Thirty-Four Thousand Eight Hundred and Sixty Two.
\nIn Indian continent Number Scale : <\/strong> Sixty Seven Lakhs Thirty Four Thousand Eight Hundred and Sixty Two.<\/div>\n
\n<\/a><\/p>\nLimitations in Previous UDF<\/h2>\n
\nFor Example <\/strong>One Thousand Billion or Ten Thousand Billion or Hundred Thousand Billion or One Million Billion etc.<\/em><\/p>\n1# VBA function to Spell Numbers to Letters<\/h1>\n
\nMost importantly, <\/strong> this function accepts whole number as an input. It does not accept decimal numbers. Therefore, if you want to convert a decimal number into words, you can split whole and decimal numbers and call this function separately. This is what I have done in SpellCurrency<\/em> functions below. You can refer to them below.<\/p>\n\nFunction NumToLettersX(ByVal num As String, _\n Optional NumLetters As String = \"\", _\n Optional UnitCounter As Integer = 0) As String\n \n' Add the unit until where you want to use\n' in Number Scale - Like\n' Arab, Kharab, etc. [in Indian Continent Number Scale System]\n Dim Unit As Variant\n Unit = Array( _\n \"\", _\n \" Thousand \", _\n \" Lakhs \", _\n \" Crore \")\n \n Dim iCount As Integer: iCount = 1\n \n Do While num <> \"\"\n \n' Check if you have reached at the last Unit\n' defined in the array - Unit\n' if yes, call this same function by providing\n' the remaining digits\n \n If (iCount = UBound(Unit) + 1 And Len(num) > 2) Then\n NumLetters = Unit(UnitCounter) & NumLetters\n NumToLettersX num, NumLetters\n Exit Do\n End If\n \n If (iCount = 1) Then\n Temp = GetHundreds(Right(num, 3))\n Else\n Temp = GetHundreds(Right(num, 2))\n End If\n If (Temp <> \"Zero\") Then NumLetters = Temp & Unit(UnitCounter) & NumLetters\n \n If Len(num) > 2 And iCount = 1 Then\n num = Left(num, Len(num) - 3)\n ElseIf Len(num) >= 2 And iCount > 1 Then\n num = Left(num, Len(num) - 2)\n Else\n num = \"\"\n End If\n \n iCount = iCount + 1\n UnitCounter = UnitCounter + 1\n Loop\n NumToLettersX = Application.WorksheetFunction.Trim(NumLetters)\n \nEnd Function\n\n<\/code><\/pre>\n
\nFunction NumToLettersY(ByVal num As String, _\n Optional NumLetters As String = \"\", _\n Optional UnitCounter As Integer = 0) As String\n \n' Add the unit until where you want to use\n' in Number Scale - Like\n' Million, Billion etc. [in American Number Scale System]\n Dim Unit As Variant\n Unit = Array( _\n \"\", _\n \" Thousand \", _\n \" Million \", _\n \" Billion \", _\n \" Trillion \")\n \n Dim iCount As Integer: iCount = 1\n \n Do While num <> \"\"\n \n ' Check if you have reached at the last Unit\n ' defined in the array - Unit\n ' if yes, call this same function by providing\n ' the remaining digits\n \n If (iCount = UBound(Unit) + 1 And Len(num) > 3) Then\n NumLetters = Unit(UnitCounter) & NumLetters\n NumToLettersY num, NumLetters\n Exit Do\n End If\n \n Temp = GetHundreds(Right(num, 3))\n\n If (Temp <> \"Zero\") Then NumLetters = Temp & Unit(UnitCounter) & NumLetters\n \n If Len(num) > 3 Then\n num = Left(num, Len(num) - 3)\n Else\n num = \"\"\n End If\n \n iCount = iCount + 1\n UnitCounter = UnitCounter + 1\n Loop\n NumToLettersY = Application.WorksheetFunction.Trim(NumLetters)\nEnd Function\n\n<\/code><\/pre>\n
Most Important:<\/h2>\n
2# VBA function to Spell Currency to Words – American Number Scale System<\/h1>\n
\n2.<\/strong> Separate the whole number part and decimal part from the input [Note:<\/strong> This is because above function accepts only whole number as input]
\n3.<\/strong> Call the Spell Number function for American Number Scale System to spell the numbers separately – 1 for the Whole number and one for the Decimal part(rounded to two digits)
\n4.<\/strong> In both converted parts, append the currency Unit and Sub-Unit, etc. in the returned Number spellings for Whole Number and Decimal part respectively.<\/p>\n\nFunction spellCurrencyY(ByVal MyNumber) As String\n\n Dim wholeNumber As String\n Dim decimalNumber As String\n Dim wholeCurrencyText As String\n Dim decimalCurrencyText As String\n Dim unitSingular As String\n Dim subUnitSingular As String\n Dim unitPlural As String\n Dim subUnitPlural As String\n \n unitSingular = \" Dollar\"\n unitPlural = \" Dollars\"\n subUnitSingular = \" Cent\"\n subUnitPlural = \" Cents\"\n \n ' separate whole number part and decimal part\n ' Note: for decimal points, first round up to 2 digits\n \n MyNumber = VBA.Format(Round(MyNumber, 2), \"#0.00\")\n \n If (VBA.InStr(1, MyNumber, \".\", vbTextCompare) > 0) Then\n wholeNumber = VBA.Left(MyNumber, VBA.InStr(1, MyNumber, \".\", vbTextCompare) - 1)\n decimalNumber = VBA.Mid(MyNumber, VBA.InStr(1, MyNumber, \".\", vbTextCompare) + 1, 2)\n Else\n wholeNumber = MyNumber\n decimalNumber = \"0\"\n End If\n \n ' get the number spelling separately\n wholeCurrencyText = NumToLettersY(wholeNumber)\n decimalCurrencyText = NumToLettersY(decimalNumber)\n \n\n Select Case wholeCurrencyText\n Case \"One\"\n wholeCurrencyText = wholeCurrencyText & unitSingular\n Case \"\"\n wholeCurrencyText = \"Zero\" & unitSingular\n Case Else\n wholeCurrencyText = wholeCurrencyText & unitPlural\n End Select\n \n Select Case decimalCurrencyText\n Case \"One\"\n decimalCurrencyText = decimalCurrencyText & subUnitSingular\n Case \"\"\n decimalCurrencyText = \"Zero\" & subUnitSingular\n Case Else\n decimalCurrencyText = decimalCurrencyText & subUnitPlural\n End Select\n spellCurrencyY = wholeCurrencyText & \" and \" & decimalCurrencyText\n End Function\n\n\n<\/code><\/pre>\n
3# VBA function to Spell Currency to Words – Indian Continent Number Scale System<\/h1>\n
\nFunction spellCurrencyX(ByVal MyNumber) As String\n\n Dim wholeNumber As String\n Dim decimalNumber As String\n Dim wholeCurrencyText As String\n Dim decimalCurrencyText As String\n Dim unitSingular As String\n Dim subUnitSingular As String\n Dim unitPlural As String\n Dim subUnitPlural As String\n \n unitSingular = \" Rupee\"\n unitPlural = \" Rupees\"\n subUnitSingular = \" Paisa\"\n subUnitPlural = \" Paise\"\n \n ' separate whole number part and decimal part\n ' Note: for decimal points, first round up to 2 digits\n \n MyNumber = VBA.Format(Round(MyNumber, 2), \"#0.00\")\n \n If (VBA.InStr(1, MyNumber, \".\", vbTextCompare) > 0) Then\n wholeNumber = VBA.Left(MyNumber, VBA.InStr(1, MyNumber, \".\", vbTextCompare) - 1)\n decimalNumber = VBA.Mid(MyNumber, VBA.InStr(1, MyNumber, \".\", vbTextCompare) + 1, 2)\n Else\n wholeNumber = MyNumber\n decimalNumber = \"0\"\n End If\n \n ' get the number spelling separately\n wholeCurrencyText = NumToLettersX(wholeNumber)\n decimalCurrencyText = NumToLettersX(decimalNumber)\n \n ' get the\n Select Case wholeCurrencyText\n Case \"One\"\n wholeCurrencyText = wholeCurrencyText & unitSingular\n Case \"\"\n wholeCurrencyText = \"Zero\" & unitSingular\n Case Else\n wholeCurrencyText = wholeCurrencyText & unitPlural\n End Select\n \n Select Case decimalCurrencyText\n Case \"One\"\n decimalCurrencyText = decimalCurrencyText & subUnitSingular\n Case \"\"\n decimalCurrencyText = \"Zero\" & subUnitSingular\n Case Else\n decimalCurrencyText = decimalCurrencyText & subUnitPlural\n End Select\n spellCurrencyX = wholeCurrencyText & \" and \" & decimalCurrencyText\n End Function\n\n<\/code><\/pre>\n
4# Generic Functions [Common for all the above functions]<\/h1>\n
\nIn below example, I have used English translation for that.
\nSimilarly, if you want to change them in any other language, you can easily change the corresponding Text part for each Numbers.<\/p>\n\n'*******************************************\n' Converts a number from 100-999 into text *\n'*******************************************\n\nFunction GetHundreds(ByVal MyNumber)\n Dim Result As String\n If Val(MyNumber) = 0 Then GetHundreds = \"Zero\": Exit Function\n MyNumber = Right(\"000\" & MyNumber, 3)\n' Convert the hundreds place.\n If Mid(MyNumber, 1, 1) <> \"0\" Then\n Result = GetDigit(Mid(MyNumber, 1, 1)) & \" Hundred \"\n End If\n \n' Convert the tens and ones place.\n If Mid(MyNumber, 2, 1) <> \"0\" Then\n Result = Result & GetTens(Mid(MyNumber, 2))\n Else\n Result = Result & GetDigit(Mid(MyNumber, 3))\n End If\n GetHundreds = Result\nEnd Function\n\n'*********************************************\n' Converts a number from 10 to 99 into text. *\n'*********************************************\nFunction GetTens(TensText)\n \n Dim Result As String\n Result = \"\" ' Null out the temporary function value.\n If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...\n Select Case Val(TensText)\n Case 10: Result = \"Ten\"\n Case 11: Result = \"Eleven\"\n Case 12: Result = \"Twelve\"\n Case 13: Result = \"Thirteen\"\n Case 14: Result = \"Fourteen\"\n Case 15: Result = \"Fifteen\"\n Case 16: Result = \"Sixteen\"\n Case 17: Result = \"Seventeen\"\n Case 18: Result = \"Eighteen\"\n Case 19: Result = \"Nineteen\"\n Case Else\n End Select\n Else ' If value between 20-99...\n Select Case Val(Left(TensText, 1))\n Case 2: Result = \"Twenty\"\n Case 3: Result = \"Thirty\"\n Case 4: Result = \"Forty\"\n Case 5: Result = \"Fifty\"\n Case 6: Result = \"Sixty\"\n Case 7: Result = \"Seventy\"\n Case 8: Result = \"Eighty\"\n Case 9: Result = \"Ninety\"\n Case Else\n End Select\n \n Result = Result & \" \" & GetDigit _\n (Right(TensText, 1)) ' Retrieve ones place.\n End If\n GetTens = Result\nEnd Function\n\n'*******************************************\n' Converts a number from 1 to 9 into text. *\n'*******************************************\n\nFunction GetDigit(Digit)\n Select Case Val(Digit)\n Case 1: GetDigit = \"One\"\n Case 2: GetDigit = \"Two\"\n Case 3: GetDigit = \"Three\"\n Case 4: GetDigit = \"Four\"\n Case 5: GetDigit = \"Five\"\n Case 6: GetDigit = \"Six\"\n Case 7: GetDigit = \"Seven\"\n Case 8: GetDigit = \"Eight\"\n Case 9: GetDigit = \"Nine\"\n Case Else: GetDigit = \"\"\n End Select\nEnd Function\n\n\n<\/code><\/pre>\n
How to use these above Functions in Excel<\/h1>\n
\n<\/a>
\nStep 1. <\/strong> Open <\/a>your Excel Workbook
\nStep 2. <\/strong> Press Alt + F11
\nStep 3. <\/strong> Add a Regular Module in Excel
\nStep 4. <\/strong> Copy and Paste the Below Code in the Module or in separate modules [your wish]
\nStep 5. <\/strong> Once you have copied and Pasted the above Code in a Module, you can use the Below Formula in your Workbook in any WorkSheet as shown in Below Picture:
\n<\/a>[\/et_pb_text][et_pb_cta title=”FREE DOWNLOAD – Numbers to Words Convertor” button_url=”\/excel\/wp-content\/downloads\/SpellCurrency-v-0.2.xls” button_text=”Download FREE Workbook” _builder_version=”4.17.6″ _module_preset=”a50a16dd-d05f-4ea2-acab-1468d2e4010e” global_colors_info=”{}”]<\/p>\n
Of course, you can share your customized version here for other visitors who may get benefited \u2013 after all sharing is caring !!
Currently, this excel file has two user-defined functions \u2013 One for the Indian Number Scale System and the other for the American English Scale system.<\/p>\n\n