วันพุธที่ 6 เมษายน พ.ศ. 2559

แปลงตัวเลขเป็นคำอ่านภาษาอังกฤษ (Convert a numeric value into English words in Excel 2016)

ไม่ได้อัพเดทบทความเสียนานหลายเดือน เนื่องจากมีงานอื่นต้องทำมากโข วันนี้เราจะนำเสนอเรื่องของ VBA Excel กันเสียที...ช่วงก่อนหน้านี้จะอัพ... thumbnail 1 summary


ไม่ได้อัพเดทบทความเสียนานหลายเดือน เนื่องจากมีงานอื่นต้องทำมากโข วันนี้เราจะนำเสนอเรื่องของ VBA Excel กันเสียที...ช่วงก่อนหน้านี้จะอัพเดทเรื่องของการใช้งานฟังก์ชั่น Excel หรือ สูตร Excel เสียมากกว่า ไม่ค่อยได้เกี่ยวข้องกับการใช้ VBA เลย...โดยเนื้อหาในวันนี้จะเป็นเรื่องของ การแปลงตัวเลขให้เป็นคำอ่านภาษาอังกฤษ...

โดยปกติแล้ว Excel มีฟังก์ชั่นที่ชื่อว่า Bahttext เพื่อใช้เปลี่ยนตัวเลขให้เป็นคำอ่านแบบภาษาไทย เช่น

=Bahttext(50) กด Enter ก็จะได้ ห้าสิบบาทถ้วน

=Bahttext(70) กด Enter ก็จะได้ เจ็ดสิบบาทถ้วน

=Bahttext(153.25) กด Enter ก็จะได้ หนึ่งร้อยห้าสิบสามบาทยี่สิบห้าสตางค์ อย่างนี้เป็นต้น

คำถามคือว่า แล้วเราจะทำยังไงให้ มันอ่านตัวเลขเป็นภาษาอังกฤษ เช่น

เลข 100 มันก็จะอ่านว่า One Hundred

เลข 10 มันก็จะอ่านว่า Ten

เลข 5 มันก็จะอ่านว่า Five  อย่างนี้เป็นต้น

จากปัญหานี้ เมื่อเราลองเช็คที่ Excel เราพบว่า ไม่มีฟังก์ชั่นใดๆเลยใน Excel ที่สามารถแปลงตัวเลขให้เป็นคำอ่านภาษาอังกฤษ...ทางออกของเรื่องนี้คือ VBA หรือ การเขียนโปรแกรมควบคุม Excel ด้วย โปรแกรม Visual Basic for Application ที่ฝั่งอยู่ใน Microsoft Office Excel ทุกเวอร์ชั่น

ขั้นตอนการใช้งาน เขียนโปแกรม แปลงตัวเลขเป็นคำอ่านภาษาอังกฤษ

1 เปิดโปรแกรม Microsoft Excel ขึ้นมา

2 พิมพ์ 10 ที่เซลล์ A1 แล้วกด Enter
   พิมพ์ 50.25 ที่เซลล์ A2 แล้วกด Enter

   พิมพ์ 1720.03 ที่เซลล์ A3 แล้วกด Enter

   พิมพ์ 29384 ที่เซลล์ A4 แล้วกด Enter
   พิมพ์ 5 ที่เซลล์ A5 แล้วกด Enter
เราก็จะได้ข้อมูลดังภาพด้านล่างนี้...ให้เราขยายคอลัมน์ B
ให้มีความกว้างพอสมควรเพื่อรองรับคำอ่านที่อาจจะมีความยาวเกินขอบเขตคอลัมน์ (ดูภาพประกอบ)



3. กด ALT ค้างไว้แล้วกดปุ่ม F11 แล้วปล่อยปุ่มทั้งสอง
   โปรแกรม Microsoft Visual Basic จะเปิดตัวขึ้นมาตามภาพด้านล่างนี้


4 คลิ้กขวาบริเวณพื้นที่สีขาวว่างๆ แล้วคิล้กที่คำสั่ง


5 Copy โค๊ดด้านล่างนี้

Option Explicit

'Main Function

Function ConvertNumberToWord(ByVal MyNumber)

    Dim Dollars, Cents, Temp
    Dim DecimalPlace, Count
    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 cents and set MyNumber to dollar amount.
    If DecimalPlace > 0 Then
        Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
                  "00", 2))
        MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
    End If
    Count = 1
    Do While MyNumber <> ""
        Temp = GetHundreds(Right(MyNumber, 3))
        If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
        If Len(MyNumber) > 3 Then
            MyNumber = Left(MyNumber, Len(MyNumber) - 3)
        Else
            MyNumber = ""
        End If
        Count = Count + 1
    Loop
    Select Case Dollars
        Case ""
            Dollars = "No Dollars"
        Case "One"
            Dollars = "One Dollar"
         Case Else
            Dollars = Dollars & " Dollars"
    End Select
    Select Case Cents
        Case ""
            Cents = " "
        Case "One"
            Cents = " and One Cent"
              Case Else
            Cents = " and " & Cents & " Cents"
    End Select
    ConvertNumberToWord = Dollars & Cents
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
        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

6 คลิกขวาที่พื้นที่ขาวๆ บน Module แล้วคลิ้กวาง โค้ดลงไปก็จะได้โค้ดเข้าไปใน Module ดังภาพด้านล่างนี้




7 กด ALT+F11 อีกครั้งเพื่อกลับมาที่ Excel

8 ที่ เซลล์ B1 พิมพ์สูตรว่า  =ConvertNumberToWord(A1)

  แล้ว Copy สูตรลงมาถึง เซลล์ B5 เราก็จะได้คำตอบดังภาพด้านล่างนี้