Excel VBA – Convert Numbers to Words in Hindi Language

Here is the UDF to Convert Numbers to Words in Hindi Language. This can be extended for other Languages as well with little modifications, do let me know your thoughts in comments.

How to create the function Called SpellNumber()
1) Start Microsoft Excel.
2) Press ALT+F11 to start the Visual Basic Editor.
3) On the Insert menu, click Module.
4) Copy the following code into the module sheet.

Save the file as Excel 97-2003 (XLS) or Excel Macro Enabled (XLSM) Workboook, otherwise you will lose the macro. [Enclosed sample file at bottom for reference]

Related: How to convert Numbers to Words in Indian Currency Format – VBA Macros

|Main Function

Function SpellNumberInHindi(ByVal MyNumber, _
        Optional incRupees As Boolean = True)
    Dim Crore, Lac, Rupees, Paise, Temp
    Dim DecimalPlace As Long, Count As Long
    Dim myLac, myCrore
    ReDim Place(9) As String
    
    Place(2) = " |Thousand| ": Place(3) = " Million "
    Place(4) = " Billion ": Place(5) = " Trillion "
    
    ' String representation of amount.
    MyNumber = Trim(Str(MyNumber))
    
    ' Position of decimal place 0 if none.
    DecimalPlace = InStr(MyNumber, ".")
    
    ' Convert Paise and set MyNumber to Rupees amount.
    If DecimalPlace > 0 Then
        Paise = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
        MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
    End If
    
    myCrore = MyNumber \ 10000000
    myLac = (MyNumber - myCrore * 10000000) \ 100000
    MyNumber = MyNumber - myCrore * 10000000 - myLac * 100000
    Count = 1
    Do While myCrore <> ""
        Temp = GetHundreds(Right(myCrore, 3))
        If Temp <> "" Then Crore = Temp & Place(Count) & Crore
        If Len(myCrore) > 3 Then
            myCrore = Left(myCrore, Len(myCrore) - 3)
        Else
            myCrore = ""
        End If
        Count = Count + 1
    Loop
    Count = 1
    
    Do While myLac <> ""
        Temp = GetHundreds(Right(myLac, 3))
        If Temp <> "" Then Lac = Temp & Place(Count) & Lac
        If Len(myLac) > 3 Then
            myLac = Left(myLac, Len(myLac) - 3)
        Else
            myLac = ""
        End If
        Count = Count + 1
    Loop
    Count = 1
    
    Do While MyNumber <> ""
        Temp = GetHundreds(Right(MyNumber, 3))
        If Temp <> "" Then Rupees = Temp & Place(Count) & Rupees
        If Len(MyNumber) > 3 Then
            MyNumber = Left(MyNumber, Len(MyNumber) - 3)
        Else
            MyNumber = ""
        End If
        Count = Count + 1
    Loop
    
    Select Case Crore
        Case "": Crore = ""
        Case "One": Crore = " One |Crore| "
        Case Else: Crore = Crore & " |Crore| "
    End Select
         
    Select Case Lac
        Case "": Lac = ""
        Case "One": Lac = " One |Lac| "
        Case Else: Lac = Lac & " |Lac| "
    End Select
    Select Case Rupees
        Case "": Rupees = "Zero "
        Case "One": Rupees = "One "
        Case Else:
        
        Rupees = Rupees
    End Select
    SpellNumberInHindi = TranslateText(IIf(incRupees, "", "") & _
                         Crore & Lac & Rupees)
    
End Function

| Support Functions

' Converts a number from 100-999 into text
Function GetHundreds(ByVal MyNumber)
    Dim Result As String
    If Val(MyNumber) = 0 Then Exit Function
    MyNumber = Right("000" & MyNumber, 3)
        
    ' Convert the hundreds place.
    If Mid(MyNumber, 1, 1) <> "0" Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred| "
    End If
        
    ' Convert the tens and ones place.
    If Mid(MyNumber, 2, 1) <> "0" Then
        Result = Result & GetTens(Mid(MyNumber, 2))
    Else
        Result = Result & GetDigit(Mid(MyNumber, 3))
    End If
    GetHundreds = Result
End Function
' Converts a number from 10 to 99 into text.
Function GetTens(TensText)
    Dim Result As String
    Result = "" ' Null out the temporary function value."
    If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19…
        Select Case Val(TensText)
            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 "
            Case Else
        End Select
    Else ' If value between 20-99…
        Select Case Val(Left(TensText, 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 "
            Case Else
        End Select
    Result = Result & GetDigit _
    (Right(TensText, 1)) ' Retrieve ones place.
    End If
    GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Function GetDigit(Digit)
    Select Case Val(Digit)
        Case 1: GetDigit = "One "
        Case 2: GetDigit = "Two "
        Case 3: GetDigit = "Three "
        Case 4: GetDigit = "Four "
        Case 5: GetDigit = "Five "
        Case 6: GetDigit = "Six "
        Case 7: GetDigit = "Seven "
        Case 8: GetDigit = "Eight "
        Case 9: GetDigit = "Nine "
        Case Else: GetDigit = ""
    End Select
End Function

|Translator Function

Function TranslateText(strTextToConvert As String)
    Dim objInternetExplorer     As Object
    Dim OutPutTxt           As String

    If strTextToConvert = "" Then Exit Function
    
    If InStr(strTextToConvert, "|") = 0 Then
        strTextToConvert = strTextToConvert & "|"
    End If
    TextArray = Split(strTextToConvert, "|")
    Set objInternetExplorer = CreateObject("InternetExplorer.application")
    objInternetExplorer.Visible = False
    For iLP = LBound(TextArray) To UBound(TextArray)
        ThisString = TextArray(iLP)
        objInternetExplorer.navigate _
        "https://translate.google.com/#en/hi/" & ThisString
        Do Until objInternetExplorer.ReadyState = 4
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("0:00:05"))
        If TempTxt <> "" Then objInternetExplorer.Refresh
        Do Until objInternetExplorer.ReadyState = 4
            DoEvents
        Loop
        Application.Wait (Now + TimeValue("0:00:05"))
        OutPutTxt = objInternetExplorer.Document. _
        getElementById("result_box").innerHTML
        OutPutTxt = Replace(Replace(OutPutTxt, "", ""), "", "")
        TempTxt = TempTxt & OutPutTxt & " "
        
    Next iLP
    
    objInternetExplorer.Quit
    TranslateText = TempTxt
    Set objInternetExplorer = Nothing

End Function

Download Example File Convert_Numbers_To_Words_In_Hindi.xls

Also read

4

No Responses

Show all responses

Write a response