Thursday 5 January 2012

Convert Number in to Word up to 99 crore in EXCEL

Below is the function which gives you Rupees in Word




Function GetWord(ByVal Num)
    Dim InWord As String
    Dim Paisa As String
    Dim x As Integer
    Paisa = ""
    InWord = ""
    x = InStr(Num, ".")
    If x > 0 Then
        Paisa = GetTens(Left(Mid(Num, x + 1) & _
                  "00", 2))
              Num = Trim(Left(Num, x - 1))
              Paisa = Paisa & " Paisa"
    End If
    
    Select Case Len(Num)
        Case 0
            InWord = " Zero "
        Case 1
            InWord = GetDigit(Num)
        Case 2
            InWord = GetTens(Num)
        Case 3
            InWord = GetHundreds(Num)
        Case 4
            InWord = GetHundreds(Num)
            Num = Left(Num, 1)
            InWord = GetDigit(Num) & " Thousand " & InWord
        Case 5
            InWord = GetHundreds(Num)
            Num = Left(Num, 2)
            InWord = GetTens(Num) & " Thousand " & InWord
        Case 6
            InWord = GetHundreds(Num)
            x = Right(Left(Num, 3), 2)
            InWord = GetTens(x) & " Thousand " & InWord
            Num = Left(Num, 1)
            InWord = GetDigit(Num) & " Lac " & InWord
        Case 7
            InWord = GetHundreds(Num)
            x = Right(Left(Num, 4), 2)
            InWord = GetTens(x) & " Thousand " & InWord
            Num = Left(Num, 2)
            InWord = GetTens(Num) & " Lac " & InWord
        Case 8
            x = Right(Num, 3)
            InWord = GetHundreds(Num)
            x = Right(Left(Num, 5), 2)
            InWord = GetTens(x) & " Thousand " & InWord
            x = Right(Left(Num, 3), 2)
            InWord = GetTens(x) & " Lac " & InWord
            Num = Left(Num, 1)
            InWord = GetDigit(Num) & " Crore " & InWord
        Case 9:
            Dim wrd As String
            wrd = Num
            x = Right(Num, 3)
            InWord = GetHundreds(Num)
            Num = Num / 1000    'Number length is greater then 8 so number must be truncated
            x = InStr(Num, ".") 'to length <=8
            Num = Trim(Left(Num, x - 1))
            x = Right(Num, 2)
            InWord = GetTens(x) & " Thousand " & InWord
            x = Right(Left(Num, 4), 2)
            InWord = GetTens(x) & " Lac " & InWord
            Num = Left(Num, 2)
            InWord = GetTens(Num) & " Crore " & InWord
        Case Is >= 10:
            InWord = "Rupees >= 100 Crore"
            
    End Select
    If InWord <> "Rupees >= 100 Crore" Then
        InWord = "Rupees " & InWord
        If Paisa <> "" Then
            InWord = InWord & " And "
        End If
        GetWord = InWord & Paisa & " Only"
        Else
        GetWord = InWord
    End If
End Function


      '*******************************************
      ' 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


              If Right(TensText, 1) > 0 Then
              Result = Result & GetDigit _
                  (Right(TensText, 1))  ' Retrieve ones place.
                  End If
          End If
          GetTens = Result
      End Function


      '*******************************************
      ' Converts a number from 1 to 9 into text. *
      '*******************************************

      Function GetDigit(Digit)
          Select Case Val(Digit)
              Case 0: GetDigit = "Zero"
              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


To Use this function you have to use VB Editor in your Excel File.
Right Click on Sheet Name (Default is SHEET1) and in it select View Code


Now you'll have VB Editor.
Right Click on VBAProjetc  and select Insert.
in Insert select Module.
Now, you'll have Module1.
Double click on Module1 to open it.
Now, paste above functions.

Now, you have successfully created InWord function to convert number into word.






No comments:

Post a Comment

Search This Blog