วิธีการแปลงค่าตัวเลขเป็นคำภาษาอังกฤษใน Excel

หมายเลขบทความ (Article ID): 213360 - ผลิตภัณฑ์ที่เกี่ยวข้องในบทความนี้
ขยายทั้งหมด | ยุบทั้งหมด

เนื้อหาบนหน้านี้

สรุป

บทความนี้มีฟังก์ชันตัวอย่าง Microsoft Visual Basic for Applications ซึ่งคุณสามารถใช้ในการแปลงค่าตัวเลขที่อยู่ในเซลล์บนแผ่นงาน Microsoft Excel ให้เป็นคำที่มีความหมายเทียบเท่าในภาษาอังกฤษ

ข้อมูลเพิ่มเติม

Microsoft แสดงตัวอย่างการเขียนโปรแกรมเพื่อประกอบการอธิบายเท่านั้น และไม่มีการรับประกัน ไม่ว่าโดยชัดแจ้งหรือโดยนัย ทั้งนี้รวมถึง แต่ไม่จำกัดเพียงการรับประกันโดยนัยในเรื่องการซื้อขายหรือความเหมาะสมสำหรับวัตถุประสงค์เฉพาะ บทความนี้อนุมานว่าคุณมีความคุ้นเคยกับภาษาการเขียนโปรแกรมที่แสดงอยู่ รวมทั้งเครื่องมือต่างๆ ที่ใช้ในการสร้างและแก้จุดบกพร่องกระบวนการ วิศวกรฝ่ายสนับสนุนของ Microsoft สามารถช่วยอธิบายเกี่ยวกับการทำงานของกระบวนการเฉพาะได้ อย่างไรก็ตาม จะไม่มีการปรับเปลี่ยนตัวอย่างเหล่านี้เพื่อให้มีหน้าที่การใช้งานที่เพิ่มขึ้น หรือสร้างกระบวนการใดๆ เพื่อตอบสนองความต้องการอย่างใดอย่างหนึ่งของคุณโดยเฉพาะ

วิธีการสร้างฟังก์ชันตัวอย่าง Called SpellNumber

  1. เริ่ม Microsoft Excel
  2. กด ALT+F11 เพื่อเริ่ม Visual Basic Editor
  3. ในเมนู แทรก คลิก โมดูล
  4. พิมพ์รหัสต่อไปนี้ในแผ่นงานโมดูล
    Option Explicit
    'Main Function
    Function SpellNumber(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 = " and No Cents"
            Case "One"
                Cents = " and One Cent"
                  Case Else
                Cents = " and " & Cents & " Cents"
        End Select
        SpellNumber = 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
    					

วิธีการใช้ฟังก์ชันตัวอย่าง SpellNumber

เมื่อต้องการใช้ฟังก์ชันตัวอย่างในการเปลี่ยนตัวเลขให้เป็นข้อความที่เขียน ให้ใช้วิธีการอย่างใดอย่างหนึ่งที่แสดงในตัวอย่างต่อไปนี้:

วิธีที่ 1: การป้อนข้อมูลโดยตรง

คุณสามารถเปลี่ยน 32.50 เป็น "Thirty Two Dollars and Fifty Cents" โดยการป้อนสูตรต่อไปนี้ลงในเซลล์:
=SpellNumber(32.50)

วิธีที่ 2: การอ้างอิงเซลล์

คุณสามารถอ้างถึงเซลล์อื่นๆ ในสมุดงาน ตัวอย่างเช่น ป้อนตัวเลข 32.50 ลงในเซลล์ A1 และพิมพ์สูตรต่อไปนี้ลงในอีกเซลล์หนึ่ง:
=SpellNumber(A1)

วิธีที่ 3: วางฟังก์ชัน หรือ แทรกฟังก์ชัน

คุณสามารถใช้ วางฟังก์ชัน ใน Excel 2000 และใน Excel 2002 หรือคุณสามารถใช้ แทรกฟังก์ชัน ใน Excel 2003 เพื่อป้อนฟังก์ชันแบบกำหนดเองลงในแผ่นงาน

Excel 2000 และ Excel 2002

เมื่อต้องการใช้ วางฟังก์ชัน ให้ทำตามขั้นตอนเหล่านี้:
  1. เลือกเซลล์ที่คุณต้องการ
  2. คลิก วางฟังก์ชัน บนแถบเครื่องมือ มาตรฐาน
  3. ภายใต้ ประเภทฟังก์ชัน คลิก ผู้ใช้กำหนดเอง
  4. ภายใต้ ชื่อฟังก์ชัน คลิก SpellNumber แล้วคลิก ตกลง
  5. ป้อนตัวเลขหรือการอ้างอิงเซลล์ที่คุณต้องการ แล้วคลิก ตกลง
  6. คลิก เสร็จสิ้น

Excel 2003

เมื่อต้องการใช้ แทรกฟังก์ชัน ให้ทำตามขั้นตอนเหล่านี้:
  1. เลือกเซลล์ที่คุณต้องการ
  2. คลิก แทรกฟังก์ชัน บนแถบเครื่องมือ มาตรฐาน
  3. ภายใต้ หรือเลือกประเภท คลิก ผู้ใช้กำหนดเอง
  4. ในรายการ เลือกฟังก์ชัน คลิก SpellNumber แล้วคลิก ตกลง
  5. ป้อนตัวเลขหรือการอ้างอิงเซลล์ที่คุณต้องการ แล้วคลิก ตกลง

คุณสมบัติ

หมายเลขบทความ (Article ID): 213360 - รีวิวครั้งสุดท้าย: 4 มกราคม 2551 - Revision: 7.0
ใช้กับ
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
Keywords: 
kbhowto kbprogramming kbdtacode KB213360

ให้ข้อเสนอแนะ