Excel 沒有在工作表中將數字顯示為英文文字的預設函數,但您可以將下列 SpellNumber 函數程式代碼貼到 VBA (Visual Basic for Applications) 模組中,藉此新增此功能。 此函數可讓您使用公式將貨幣和 cent 金額轉換成文字,因此 22.50 會讀為 Twenty-Two Dollars and Fifty Cents。。 如果您使用 Excel 做為列印檢查的範本,這項功能就非常有用。
如果您想要將數值轉換成文字格式,而不將其顯示為文字,請改用 TEXT 函數 。
附註: Microsoft 僅提供圖例的程式設計範例,不含明示或暗示的擔保。 這包括 (但不限於) 適用于特定目的之可操作性或適用性的暗示擔保。 本文假設您熟悉 VBA 程式設計語言,以及用來建立和偵錯程式的工具。 Microsoft 支援工程師可以協助說明特定程式的功能。 不過,他們不會修改這些範例以提供新增功能,或建構程式以符合您的特定需求。
建立 SpellNumber 函數,將數字轉換成文字
-
使用鍵盤快捷方式 Alt + F11 開啟 VBE) (Visual Basic 編輯器。
附註: 您也可以在功能區中顯示 [開發人員] 索引標籤,藉此存取 Visual Basic 編輯器。
-
按一下 [插入] 索引標籤,再按一下 [模組]。
-
複製下列程式碼行。
附註: 此程式代碼稱為 使用者定義函數 (UDF),可將數位轉換成文字的工作自動化。
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
-
將上述程式碼行貼入 [模組1 (程式碼)] 方塊中。
-
按 Alt + Q 傳回 Excel。 現在即可使用 SpellNumber 函數。
附註: 此函數僅適用於目前的活頁簿。 若要在另一個活頁簿中使用此函數,您必須重複這些步驟,以複製並貼上該活頁簿中的程序代碼。
在個別儲存格中使用 SpellNumber 函數
-
將公式 =SpellNumber (A1) 輸入您要顯示書面數位的單元格,其中 A1 是包含您要轉換之數位的單元格。 您也可以手動輸入像是 =SpellNumber (22.50) 值。
-
按 Enter 以確認公式。
儲存您的 SpellNumber 函數活頁簿
Excel 無法將含有宏函數的活頁簿儲存為標準的無宏活頁簿格式 (.xlsx) 。 若按一下 [檔案] > [儲存]。 [VB 專案] 對話方塊隨即開啟。 按一下 [否]。
您可以將檔案儲存為 Excel Macro-Enabled 活頁簿 (.xlsm) ,將檔案保留為目前的格式。
-
按一下 [檔案] > [另存新檔]。
-
單擊 [ 存盤類型] 下拉功能表,然後選 取 [Excel Macro-Enabled 活頁簿]。
-
按一下 [儲存]。