如何在 Excel 中将数值转换为英文单词

本文包含 Microsoft Visual Basic for Applications 函数示例,您可以使用这些函数将 Microsoft Excel 工作表单元格中的数值转换为等效的英文单词。

如何在 Excel 中将数值转换为英文单词

创建名为 SpellNumber 的示例函数

(01)启动 Microsoft Excel-----按 Alt+F11 启动 Visual Basic 编辑器-----在“插入”菜单上,单击“模块”

如何在 Excel 中将数值转换为英文单词 第2张
如何在 Excel 中将数值转换为英文单词 第3张

(02)在模块表中键入下面的代码

(03)Option Explicit'Main FunctionFunction SpellNumber(ByVal MyNumber)Dim Dollars, Cents, TempDim DecimalPlace, CountReDim Place(9) As StringPlace(2) = " Thousand "Place(3) = " Million "Place(4) = " Billion "Place(5) = " Trillion "' String representation of mber = Trim(Str(MyNumber))' Position of decimal place 0 if malPlace = InStr(MyNumber, ".")' Convert cents and set MyNumber to dollar DecimalPlace > 0 ThenCents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & _                  "00", 2))MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))End IfCount = 1Do While MyNumber <> ""Temp = GetHundreds(Right(MyNumber, 3))If Temp <> "" Then Dollars = Temp & Place(Count) & DollarsIf Len(MyNumber) > 3 ThenMyNumber = Left(MyNumber, Len(MyNumber) - 3)ElseMyNumber = ""End IfCount = Count + 1LoopSelect Case DollarsCase ""Dollars = "No Dollars"Case "One"Dollars = "One Dollar"Case ElseDollars = Dollars & " Dollars"End SelectSelect Case CentsCase ""Cents = " and No Cents"Case "One"Cents = " and One Cent"Case ElseCents = " and " & Cents & " Cents"End SelectSpellNumber = Dollars & CentsEnd Function      ' Converts a number from 100-999 into text Function GetHundreds(ByVal MyNumber)Dim Result As StringIf Val(MyNumber) = 0 Then Exit FunctionMyNumber = Right("000" & MyNumber, 3)' Convert the hundreds Mid(MyNumber, 1, 1) <> "0" ThenResult = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "End If' Convert the tens and ones Mid(MyNumber, 2, 1) <> "0" ThenResult = Result & GetTens(Mid(MyNumber, 2))ElseResult = Result & GetDigit(Mid(MyNumber, 3))End IfGetHundreds = ResultEnd Function      ' Converts a number from 10 to 99 into text. Function GetTens(TensText)Dim Result As StringResult = ""           ' Null out the temporary function Val(Left(TensText, 1)) = 1 Then   ' If value between ct 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 ElseEnd SelectElse                                 ' If value between ct 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 ElseEnd SelectResult = Result & GetDigit _(Right(TensText, 1))  ' Retrieve ones IfGetTens = ResultEnd 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 SelectEnd Function

(04)关闭即可

使用 SpellNumber 示例函数

(01)方法 1:直接输入通过将下面的公式输入单元格中,可以将 32.50 更改为“Thirty Two Dollars and Fifty Cents”:=SpellNumber(32.50)

(02)方法 2:单元格引用可以引用工作簿中的其他单元格。例如,在单元格 A1 中输入数字 32.50,然后在另一单元格中键入下面的公式:=SpellNumber(A1)

(03)方法 3:插入函数Excel 2003:选择所需的单元格。单击“常用”工具栏中的“插入函数”。在“或选择类别”下,单击“用户定义”。在“选择函数”列表中,单击“SpellNumber”,然后单击“确定”。输入所需的数字或单元格引用,然后单击“确定”。Excel 2007 和 2010:选择所需的单元格。单击“公式”功能区上的“插入函数”。在“或选择类别”下,单击“用户定义”。在“选择函数”列表中,单击“SpellNumber”,然后单击“确定”。输入所需的数字或单元格引用,然后单击“确定”。

(04)如图

如何在 Excel 中将数值转换为英文单词 第4张