How To Convert Figures Into Words In Excel?

How To Convert Figures Into Words In Excel

How To Convert Figures Into Words In Excel?

Learn how to convert figures into words in Excel using custom functions or VBA code, easily transforming numerical values like “1234” into text representations like “One Thousand Two Hundred and Thirty Four.” This guide provides a step-by-step process to automate this conversion and enhance your spreadsheet capabilities.

Introduction: The Power of Numerical-to-Text Conversion

Excel is a powerhouse for data manipulation, but sometimes, you need to present numerical data in a more readable, text-based format. Imagine creating invoices, checks, or financial reports where amounts are required to be displayed both numerically and in words. The ability to convert figures into words significantly enhances clarity and minimizes potential errors. This conversion is not a built-in Excel function, but it can be readily achieved through custom functions written in VBA (Visual Basic for Applications).

Why Convert Figures Into Words? The Benefits

Converting figures into words offers several key advantages:

  • Enhanced Clarity: Clearly stating numerical values in words alongside figures reduces ambiguity.
  • Error Prevention: In financial documents like checks or invoices, having the amount spelled out can prevent fraudulent alterations.
  • Professionalism: It adds a professional touch to documents like reports and presentations.
  • Accessibility: Helps people who might find it easier to understand numbers expressed in words.
  • Legal Compliance: Some documents may legally require amounts to be written out in words.

The VBA Approach: A Step-by-Step Guide

Here’s a detailed guide on how to convert figures into words in Excel using VBA:

  1. Open the VBA Editor: Press Alt + F11 to open the Visual Basic Editor.

  2. Insert a New Module: In the VBA Editor, go to Insert > Module.

  3. Paste the VBA Code: Paste the following VBA code into the module.

    Function ConvertNumberToWords(ByVal MyNumber) As String
    Dim Rupees, Paise As String
    Dim DecimalPlace, Count
    ReDim Place(9) As String
    Place(2) = " Thousand "
    Place(3) = " Million "
    Place(4) = " Billion "
    Place(5) = " Trillion "
    ' String representation of amount.
    MyNumber = Trim(MyNumber)
    ' Round to two decimals
    MyNumber = Round(MyNumber, 2)
    MyNumber = Trim(Str(MyNumber))
    ' Position of decimal place 0 if none.
    DecimalPlace = InStr(MyNumber, ".")
    ' Convert amount before decimal place to words.
    If DecimalPlace > 0 Then
        Rupees = ConvertCents(Left(MyNumber, DecimalPlace - 1))
        Paise = GetTens(Mid(MyNumber, DecimalPlace + 1) & "0")
    Else
        Rupees = ConvertCents(MyNumber)
    End If
    ' Convert amount after decimal place to words.
    Select Case Paise
        Case ""
            ConvertNumberToWords = Rupees & " Rupees"
        Case "One"
            ConvertNumberToWords = Rupees & " Rupees And " & Paise & " Paise"
        Case Else
            ConvertNumberToWords = Rupees & " Rupees And " & Paise & " Paise"
    End Select
    End Function
    ' Converts a number from 1-999 into text
    Function ConvertCents(ByVal MyNumber) As String
    Dim Result As String
    Dim Temp
    Dim Rupees
    ReDim Place(9) As String
    Place(2) = " Thousand "
    Place(3) = " Million "
    Place(4) = " Billion "
    Place(5) = " Trillion "
    ' String representation of amount.
    MyNumber = Trim(MyNumber)
    ' Convert amount before decimal place to words.
    If MyNumber = 0 Then
       ConvertCents = "Zero"
       Exit Function
    End If
    
    Dim Count As Integer
    
    Count = 1
    Do While MyNumber <> 0
        Temp = MyNumber
        MyNumber = MyNumber  1000
        If Temp - MyNumber  1000 <> 0 Then
            Result = GetHundreds(Temp - MyNumber  1000) & Place(Count) & Result
        End If
        Count = Count + 1
    Loop
    
    ConvertCents = Result
    End Function
    
    ' Converts a number from 10-99 into text.
    Function GetTens(ByVal MyNumber) As String
    Dim Result As String
    Result = ""   ' Nullify result
    ' If value between 10-19...
    If MyNumber < 10 Then
         GetTens = GetOnes(MyNumber)
    End If
    
    If MyNumber < 20 Then
        Select Case MyNumber
            Case 10: Result = "Ten"
            Case 11: Result = "Eleven"
            Case 12: Result = "Twelve"
            Case 13: Result = "Thirteen"
            Case 14: Result = "Fourteen"
            Case 15: Result = "Fifteen"
            Case 16: Result = "Sixteen"
            Case 17: Result = "Seventeen"
            Case 18: Result = "Eighteen"
            Case 19: Result = "Nineteen"
        End Select
        GoTo ExitFunction
    End If
    
    ' Else if value between 20-99...
    Select Case Left(MyNumber, 1)
        Case "2": Result = "Twenty "
        Case "3": Result = "Thirty "
        Case "4": Result = "Forty "
        Case "5": Result = "Fifty "
        Case "6": Result = "Sixty "
        Case "7": Result = "Seventy "
        Case "8": Result = "Eighty "
        Case "9": Result = "Ninety "
    End Select
    Result = Result & GetOnes(Right(MyNumber, 1))
    ' Exit Function:
    ExitFunction:
    GetTens = Result
    End Function
    
    ' Converts a number from 100-999 into text.
    Function GetHundreds(ByVal MyNumber) As String
    Dim Result As String
    If MyNumber > 99 Then
        Result = GetOnes(Left(MyNumber, 1)) & " Hundred " & GetTens(Right(MyNumber, 2))
    Else
        Result = GetTens(MyNumber)
    End If
    GetHundreds = Result
    End Function
    
    ' Converts a number from 1-9 into text.
    Function GetOnes(ByVal MyNumber) As String
    Dim Result As String
    Select Case MyNumber
        Case 1: Result = "One"
        Case 2: Result = "Two"
        Case 3: Result = "Three"
        Case 4: Result = "Four"
        Case 5: Result = "Five"
        Case 6: Result = "Six"
        Case 7: Result = "Seven"
        Case 8: Result = "Eight"
        Case 9: Result = "Nine"
    End Select
    GetOnes = Result
    End Function
    
  4. Close the VBA Editor: Close the VBA editor window.

  5. Use the Custom Function: In your Excel worksheet, use the function like this: =ConvertNumberToWords(A1), where A1 contains the numerical value you want to convert.

Customizing the Code for Specific Needs

The provided VBA code can be tailored to fit specific requirements, such as:

  • Currency: Modify the code to include specific currency symbols or names (e.g., Dollars, Euros).
  • Decimal Precision: Adjust the rounding and decimal handling to meet your desired level of precision.
  • Language: Adapt the code to different languages by translating the number names.

Common Mistakes and Troubleshooting

When implementing this solution, some common pitfalls include:

  • Typos in the Code: Carefully check for typos in the VBA code, especially in the Select Case statements.
  • Incorrect Cell References: Ensure that the cell reference in the Excel formula (e.g., A1) is correct.
  • Security Settings: Excel’s security settings may prevent the VBA code from running. Ensure that macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select “Enable all macros (not recommended; potentially dangerous code can run)” or “Disable all macros except digitally signed macros.”

Examples

Number Formula Result
123 =ConvertNumberToWords(123) One Hundred Rupees and Twenty Three Rupees
1234 =ConvertNumberToWords(1234) One Thousand Rupees and Two Hundred Rupees and Thirty Four Rupees
1234.56 =ConvertNumberToWords(1234.56) One Thousand Rupees and Two Hundred Rupees and Thirty Four Rupees and Fifty Six Paise

Ensuring Accuracy and Consistency

  • Always test the function with a wide range of numbers, including integers, decimals, and large values.
  • Double-check the results against manual conversions to verify accuracy.
  • Document the function and any customizations made to ensure consistency across different workbooks.

Frequently Asked Questions (FAQs)

What versions of Excel are compatible with this VBA code?

The provided VBA code is generally compatible with most versions of Excel, including Excel 2010, 2013, 2016, 2019, and Microsoft 365. Minor adjustments might be necessary for older versions. It’s always a good idea to test the code on your specific Excel version to ensure compatibility.

Can I convert figures into words in other languages using this method?

Yes, you can adapt the VBA code to other languages by modifying the text strings within the Select Case statements and updating the logic for handling different number formats. This will require a good understanding of both VBA and the target language’s number naming conventions.

How do I handle very large numbers using this code?

The provided code includes support for trillions. To handle even larger numbers, you would need to extend the Place array and add logic to manage larger denominations like quadrillions and quintillions. Be mindful of the limitations of Excel and VBA when dealing with extremely large numbers.

Is there a limit to the number of digits I can convert?

While Excel can handle large numbers, VBA’s Double data type has limitations. Typically, you can accurately convert numbers up to 15 digits. Beyond that, you may encounter rounding errors. Consider using a different data type, such as String, if you need to handle extremely large numbers.

Can I customize the currency displayed in the output?

Yes, the code can be easily customized to display different currencies. You can modify the Rupees and Paise strings to reflect your desired currency (e.g., Dollars, Euros, Yen). Remember to adjust the spelling and grammar as needed.

How do I prevent the VBA code from being accidentally deleted?

To protect your VBA code, you can password-protect the VBA project. In the VBA Editor, go to Tools > VBAProject Properties > Protection and check the “Lock project for viewing” box. Enter a password and confirm it. Be sure to remember the password, as you will need it to make any future modifications.

Why am I getting a “#NAME?” error in Excel after adding the VBA code?

The “#NAME?” error usually indicates that Excel cannot find the function. This can happen if the VBA code was not correctly added to a module, if macros are disabled, or if the function name is misspelled in the Excel formula. Double-check the code, macro settings, and function name to resolve this error.

Can I use this code to convert numbers in a range of cells?

Yes, you can use the ConvertNumberToWords function to convert numbers in a range of cells by dragging the formula down or across the worksheet. For example, if you have numbers in cells A1:A10, you can enter the formula =ConvertNumberToWords(A1) in cell B1 and then drag the fill handle down to B10.

How do I enable macros in Excel?

To enable macros, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select either “Enable all macros (not recommended; potentially dangerous code can run)” or “Disable all macros except digitally signed macros.” It’s generally safer to choose the latter option and only enable macros from trusted sources.

What if I only want to convert the integer part of the number?

If you only want to convert the integer part of the number, you can use the INT function in Excel to extract the integer portion before passing it to the ConvertNumberToWords function. For example, =ConvertNumberToWords(INT(A1)).

Can I distribute my Excel file with this VBA code to others?

Yes, you can distribute your Excel file with the VBA code, but you should inform the recipients that the file contains macros and advise them to enable macros only if they trust the source. Consider digitally signing your VBA project to enhance trust and security.

How can I improve the performance of this code with large datasets?

For very large datasets, the VBA code might slow down calculations. To improve performance, you can consider:

  • Disabling automatic calculations during the conversion process (Application.Calculation = xlCalculationManual).
  • Using array formulas to perform calculations in batches.
  • Optimizing the VBA code for speed and efficiency.

Using these approaches will enable you to convert figures into words in Excel and utilize the results for a wide range of applications.

Leave a Comment