Num2En

Convert number to English words, convert 450 to 4 hundreds and fifty dollars

Function Num2En(Digit As Long, Optional Unit1 = "Dollars", Optional Unit2 = "Cents") ' Used to be called DigitToWord
 Public One(19)
 Public Ty(9)
 Public Hun(5)
 One(1) = "One "
 One(2) = " Two "
 One(3) = " Three "
 One(4) = " Four "
 One(5) = " Five "
 One(6) = " Six "
 One(7) = " Seven "
 One(8) = " Eight "
 One(9) = " Nine "
 One(10) = " Ten "
 One(11) = "Eleven "
 One(12) = "Twelve "
 One(13) = "Thirteen "
 One(14) = "Fourteen "
 One(15) = "Fifteen "
 One(16) = "Sixteen "
 One(17) = "Seventeen "
 One(18) = "Eighteen "
 One(19) = "Ninteen "
 Ty(1) = ""
 Ty(2) = "Twenty "
 Ty(3) = "Thirty "
 Ty(4) = "Fourty "
 Ty(5) = "Fifty "
 Ty(6) = "Sixty "
 Ty(7) = "Seventy "
 Ty(8) = "Eighty "
 Ty(9) = "Ninety "
 Hun(1) = "Crore(s) "
 Hun(2) = "Lakh(s) "
 Hun(3) = "Thousand "
 Hun(4) = "Hundred and "
 Dim nosplit(5)
 Dim resplit(5)
 Dim p As Integer
 Dim nos, str1 As String
 ' This Following statement is used for specify the input length 9
 nos = Trim(Digit)
 p = InStr(1, nos, ".", 1)
 pais = Mid(nos, p + 1, 2)
 If Len(pais) = 1 Then
  pais = pais + "0"
 End If
 If p > 0 Then
  nos = Mid(nos, 1, p - 1)
 End If
 nos = String((9 - Len(nos)), "0") + nos
 ' split statement
 nosplit(1) = Val(Mid(nos, 1, 2))
 nosplit(2) = Val(Mid(nos, 3, 2))
 nosplit(3) = Val(Mid(nos, 5, 2))
 nosplit(4) = Val(Mid(nos, 7, 1))
 nosplit(5) = Val(Mid(nos, 8, 2))
 For i = 1 To 5
  spli = nosplit(i)
  If spli > 0 And spli < 20 Then
   resplit(i) = Trim(One(spli)) + " "
  End If
  If spli > 19 Then
   spli1 = Val(Mid(Trim(spli), 1, 1))
   spli2 = Val(Mid(Trim(spli), 2, 1))
   resplit(i) = Trim(Ty(spli1)) + " "
   If spli2 > 0 Then
    resplit(i) = Trim(Ty(spli1)) + " " + Trim(One(spli2)) + " "
   End If
  End If
  If Not resplit(i) = "" Then
   result = result & resplit(i) & Hun(i)
  End If
 Next i
 ' paise calculations
 If p > 0 Then
  If pais > 0 And pais < 20 Then
   PAISE = Trim(One(pais)) + " "
  End If
  If pais > 19 Then
   pais1 = Val(Mid(Trim(pais), 1, 1))
   pais2 = Val(Mid(Trim(pais), 2, 1))
   PAISE = Trim(Ty(pais1)) + " "
   If pais2 > 0 Then
    PAISE = Trim(Ty(pais1)) + " " + Trim(One(pais2))
   End If
  End If
 Else
  PAISE = ""
 End If
 If p > 0 Then
  Num2En = result + " " & Unit1 + PAISE + " " & Unit2
 Else
  Num2En = Unit1 & " " + result
 End If
End Function

Digit, Optional Unit1, Optional Unit2

Views 3274 Downloads 846

'Num2En', 'DigitToWord', 'convert', 'replace', 'NotMyWork'

ANmarAmdeen
551
Texts + Strings VBA
Revisions

v1.0