# 將數字轉換成文字

Excel 沒有顯示數字為在工作表中的英文單字的預設函數，但您可以透過下列 SpellNumber 函數程式碼貼入 (Visual Basic for Applications) VBA 模組新增這項功能。此函數可讓您將元和角西非中部金額轉換成文字，以公式，因此 22.50 會標示為 20 兩個金額，為 50 分錢。這可能是非常實用，如果您使用的 Excel 另存為範本列印檢查項目。

## 建立 SpellNumber 函數，將數字轉換成文字

1. 使用鍵盤快速鍵， Alt + F11開啟 Visual Basic 編輯器 (VBE)。

附註: 您也可以在功能區中顯示 [開發人員] 索引標籤，藉此存取 Visual Basic 編輯器。

2. 按一下 [插入] 索引標籤，再按一下 [模組]。

3. 複製下列程式碼行。

附註: 稱為使用者定義函數 (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 ```
4. 將上述程式碼行貼入 [模組1 (程式碼)] 方塊中。

5. 按下Alt + Q回到 Excel。SpellNumber 函數現在已可供使用。

附註: 此函數的運作方式僅適用於目前的活頁簿。若要在另一個活頁簿中使用此函數，您必須重複複製並貼上程式碼，該活頁簿中的步驟。

## 在個別儲存格中使用 SpellNumber 函數

1. 輸入公式= SpellNumber(A1) 到您想要用來顯示撰寫的數字，儲存格 A1 在哪裡含有數字的儲存格您想要轉換。您可以手動輸入值，例如 = SpellNumber(22.50)。

2. 按下Enter以確認公式。

## 儲存您的 SpellNumber 函數活頁簿

Excel 無法儲存內含巨集函數標準無巨集的活頁簿 (.xlsx) 格式的活頁簿。如果您按一下 [檔案>儲存VB 專案] 對話方塊隨即開啟。按一下 []。

1. 按一下 [檔案] > [另存新檔]

2. 按一下 [存檔類型] 下拉式清單功能表，然後選取 [ Excel 啟用巨集活頁簿

3. 按一下 [儲存]。

×