Full Screen Home

NumToEngText(ตัวเลข)

รูปแบบการใช้สูตร  :   Download Add-In

Private Function Str2EngText(ByVal Num2Str As String, Optional Multi3 As Integer = 0) As String
' พัฒนาโดยคุณสาธิตและคุณอรวีร์
'
http://forum.onecenter.com/excel/
' September 2001

Dim vStr As String, hStr As String, tStr As String, sStr As String, txt$
Dim EngStrNumStr, EngNum2Str, EngNum3Str, EngNum4Str

EngStrNumStr = Array("", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ")

EngNum2Str = Array("", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ")

EngNum3Str = Array("", "Eleven ", "Twelve ", "Thirteen ", "Forteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ")

EngNum4Str = Array("", "Thousand ", "Million ", "Billion ")

vStr = Format(Right(Num2Str, 3), "000")
If vStr > 0 Or Multi3 = 3 Then
    txt$ = EngNum4Str(Multi3) & txt$
    hStr = Left(vStr, 1) ' 100
    tStr = Mid(vStr, 2, 1) ' 10
    sStr = Right(vStr, 1) ' 1
    If (tStr = "1" And sStr > "0") Then ' 11-19
        txt$ = EngNum3Str(sStr) & txt$
    Else
        txt$ = EngNum2Str(tStr) & EngStrNumStr(sStr) & txt$
    End If
    If (hStr > "0") Then txt$ = EngStrNumStr(hStr) & "Hundred " & txt$
End If
If Len(Num2Str) > 3 Then txt$ = Str2EngText(Left(Num2Str, Len(Num2Str) - 3), Multi3 + Multi3 * (Multi3 = 3) + 1) & " " & txt$
Str2EngText = Trim(txt$)
End Function
 

Function NumToEngText(ByVal StrNum As String, Optional ByVal fmt As Integer, Optional ByVal prcdigit As Integer = 2, Optional BahtStr As String = "BAHT", Optional StgStr As String = "STG.", Optional OnlyStr As String = "ONLY", Optional LessStr As String = "LESS") As String

Dim TxtB$, TxtS$
' StrNum = "" & Application.Round(StrNum, prcdigit) ' ถ้าต้องการปัดทศนิยม
If Left(StrNum, 1) <> "-" Then LessStr = "" Else StrNum = Mid(StrNum, 2): LessStr = LessStr & " "
TxtB$ = Str2EngText(Int(StrNum))

If (fmt And 1) Then TxtB$ = LCase(TxtB$) ' fmt : bit 1 force to lower case with only first char is capital
If TxtB$ <> Empty Then ' fmt : bit 2 Thai style text "xxxxx BAHT xxx STG."
    If InStr(BahtStr, ",") Then
        If Int(StrNum) > 1 Then BahtStr = Mid(BahtStr, InStr(BahtStr, ",") + 1) Else BahtStr = Left(BahtStr, InStr(BahtStr, ",") - 1)
    End If
    If (fmt And 2) Then TxtB$ = TxtB$ & " " & BahtStr Else TxtB$ = BahtStr & " " & TxtB$
End If

TxtS$ = Mid(StrNum & "00000", InStr(StrNum, ".") + 1, prcdigit)
If prcdigit > 0 And StrNum <> Int(StrNum) And TxtS$ > 0 Then
    If (TxtB$ <> Empty) And (fmt And 2) = 0 Then TxtB$ = TxtB$ & " AND"
    If InStr(StgStr, ",") Then
        If TxtS$ > 1 Then StgStr = Mid(StgStr, InStr(StgStr, ",") + 1) Else StgStr = Left(StgStr, InStr(StgStr, ",") - 1)
    End If
    TxtS$ = Str2EngText(TxtS$)
    If (fmt And 1) Then TxtS$ = LCase(TxtS$)
    TxtB$ = TxtB$ & " " & TxtS$ & " " & StgStr
Else
    If (TxtB$ <> Empty) Then TxtB$ = TxtB$ & " " & OnlyStr
End If
TxtB$ = LessStr & TxtB$
TxtB$ = UCase(Left(TxtB$, 1)) & Mid(TxtB$, 2)
NumToEngText = Trim(TxtB$)
End Function

รูปแบบการใช้

=NumToEngText(A, B, C, D, E, F, G)

Full Screen Home

 

http://www.tpa.or.th/xlsiam     http://xls.i.am

03/10/2544