Function Convert(ByVal mAmount As String, Optional Enclosed As Boolean)
Dim mDec As Variant, mDecWhat As String, mNumberOfDigits, mCntr, Word, OneD, TwoD, mInput
mInput = Format(mAmount, "####0.00")
OneD = Array("", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ")
TwoD = Array("", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ")
mDec = InStr(1, mInput, ".", 1)
If mDec = 0 Then mAmount = mInput 'no decimal
If mDec <> 0 Then mDecWhat = Right(mInput, Len(mInput) - mDec): mAmount = mInput: mInput = Mid(mInput, 1, mDec - 1)
If Len(mInput) > 12 Then Exit Function
If Len(mInput) <= 3 Then mNumberOfDigits = Array(mInput, "", "", "")
If Len(mInput) >= 4 And Len(mInput) <= 6 Then mNumberOfDigits = Array(Mid(mInput, 1, Len(mInput) - 3), Right(mInput, 3), "", "")
If Len(mInput) >= 7 And Len(mInput) <= 9 Then mNumberOfDigits = Array(Mid(mInput, 1, Len(mInput) - 6), Mid(mInput, Len(mInput) - 5, 3), Right(mInput, 3), "")
If Len(mInput) >= 10 And Len(mInput) <= 12 Then mNumberOfDigits = Array(Mid(mInput, 1, Len(mInput) - 9), Mid(mInput, Len(mInput) - 8, 3), Mid(mInput, Len(mInput) - 5, 3), Right(mInput, 3))
For mCntr = 0 To IIf(Len(mInput) <= 3, 0, IIf(Len(mInput) >= 4 And Len(mInput) <= 6, 1, IIf(Len(mInput) >= 7 And Len(mInput) <= 9, 2, IIf(Len(mInput) >= 10 And Len(mInput) <= 12, 3, 0))))
If Len(mNumberOfDigits(mCntr)) = 3 Then
Word = Word & OneD(Val(Left(mNumberOfDigits(mCntr), 1))) & IIf(Left(mNumberOfDigits(mCntr), 1) <> "0", "Hundred", "")
If Mid(mNumberOfDigits(mCntr), 2, 2) <= 19 Then Word = Word & " " & OneD(Val(Mid(mNumberOfDigits(mCntr), 2))) Else If Mid(mNumberOfDigits(mCntr), 2, 1) >= 1 Then Word = Word & " " & TwoD(Val(Mid(mNumberOfDigits(mCntr), 2, 1))) & IIf(Right(mNumberOfDigits(mCntr), 1) >= 1, OneD(Right(mNumberOfDigits(mCntr), 1)), "")
ElseIf Len(mNumberOfDigits(mCntr)) = 2 Then
If Val(mNumberOfDigits(mCntr)) <= 19 Then Word = Word & " " & OneD(Val(mNumberOfDigits(mCntr))) Else If Val(mNumberOfDigits(mCntr)) >= 2 Then Word = Word & " " & TwoD(Val(Left(mNumberOfDigits(mCntr), 1))) & IIf(Right(mNumberOfDigits(mCntr), 1) >= 1, OneD(Val(Right(mNumberOfDigits(mCntr), 1))), "")
ElseIf Len(mNumberOfDigits(mCntr)) = 1 Then
Word = Word & " " & OneD(Val(mNumberOfDigits(mCntr)))
End If
If Len(mInput) >= 4 And Len(mInput) <= 6 Then Word = Word & IIf(mCntr = 0 And mNumberOfDigits(0) > 0, " Thousand ", "")
If Len(mInput) >= 7 And Len(mInput) <= 9 Then Word = Word & IIf(mCntr = 0 And mNumberOfDigits(0) > 0, " Million ", IIf(mCntr = 1 And mNumberOfDigits(1) > 0, "Thousand ", ""))
If Len(mInput) >= 10 And Len(mInput) <= 12 Then Word = Word & IIf(mCntr = 0 And mNumberOfDigits(0) > 0, " Billion ", IIf(mCntr = 1 And mNumberOfDigits(1) > 0, "Million ", IIf(mCntr = 2 And mNumberOfDigits(2) > 0, "Thousand ", "")))
Next
Word = Word & IIf(Val(mAmount) > 1, " Pesos and ", " ")
If mDec <> 0 Then
If Val(mDecWhat) >= 1 And Val(mDecWhat) <= 19 Then
Word = Word & OneD(Val(mDecWhat)) & IIf(Val(mDecWhat) = 1, " Centavo", " Centavos")
ElseIf Val(mDecWhat) >= 2 Then
Word = Word & TwoD(Val(Left(mDecWhat, 1))) & IIf(Right(mDecWhat, 1) >= 1, " " & OneD(Val(Right(mDecWhat, 1))) & " Centavos", " Centavos")
End If
End If
If Val(mAmount) > 0 Then
Word = Word & " (P" & Format(mAmount, "##,##0.00") & ")": mInput = Format(mAmount, "##,##0.00")
End If
Convert = LTrim(Word): Word = ""
End Function