搜档网
当前位置:搜档网 › Excel中阿拉伯数字转英文

Excel中阿拉伯数字转英文


'1、打开Excel表,选中任意一个sheet,右键--查看代码--插入--模块,然后在模块中粘帖如下代码后保存并退出

'2、如过A1是数字,需要将英文翻译在B1,那么在B1输入:=NumbToEnglish(A1)

'3、OK,你得到了正确答案。


Function NumbToEnglish(ByVal MyNumber)
Dim Temp
Dim Inte, Dec
Dim DecimalPlace, Count

ReDim Place(9) As String
Place(2) = " THOUSAND "
Place(3) = " MILLION "
Place(4) = " BILLION "
Place(5) = " TRILLION "

' 将数字保留两位小数(四舍五入),如保留其他数位可将下面一行的中的2改为你需要保留的位数,如不做四舍五入可将下面一行注释掉

MyNumber = Round(MyNumber, 2)

' 将数字Mynumber转换成字符串格式,并去掉多余空格

MyNumber = Trim(Str(MyNumber))

If MyNumber = "" Or MyNumber = 0 Then
NumbToEnglish = "ZERO ONLY"
Else

' 查找小数点"."位置

DecimalPlace = InStr(MyNumber, ".")


' 如果找到小数点...
If DecimalPlace > 0 Then
' 转换小数部分
Temp = Len(Mid(MyNumber, DecimalPlace + 1))
Count = 1
Dec = ""
Do While Count - 1 <> Temp
Dec = Dec & " " & ConvertDecimal(Mid(MyNumber, DecimalPlace + Count, 1))
Count = Count + 1
Loop
' 去掉小数部分,保留剩下的整数部分留做转换
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))

End If

Count = 1
Do While MyNumber <> ""
' 将最后的三位数字转换成英文数字
Temp = ConvertHUNDREDs(Right(MyNumber, 3))
If Temp <> "" Then Inte = Temp & Place(Count) & Inte
If Len(MyNumber) > 3 Then
' 如果整数部分大于三位,再向前移动三位数字重复进行转换
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop


' 增加小数点描述
If Dec = "" Or Dec = "0" Then
If Inte = "" Or Dec = "0" Then
Dec = "ZERO ONLY "
End If
Else
If Inte = "" Then
Dec = "ZERO POINT" & Dec
Else
Dec = " POINT" & Dec
End If
End If
If DecimalPlace > 0 Then
NumbToEnglish = Inte & Dec
Else
NumbToEnglish = Inte
End If
End If
End Function

' 定义子函数,转换百位数

Private Function ConvertHUNDREDs(ByVal MyNumber)
Dim Result As String

' 如果数字为空,退出.
If Val(MyNumber) = 0 Then Exit Function

' 在不

满三位数的数字前补"0".
MyNumber = Right("000" & MyNumber, 3)

' 判断是否有百位数可供转换?
If Left(MyNumber, 1) <> "0" Then
If Right("000" & MyNumber, 2) <> 0 Then
Result = ConvertDigit(Left(MyNumber, 1)) & " HUNDRED AND "
Else
Result = ConvertDigit(Left(MyNumber, 1)) & " HUNDRED "
End If
End If

' 判断是否有十位数可供转换?
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & ConvertTens(Mid(MyNumber, 2))
Else
' 如果没有,转换个位数.
Result = Result & ConvertDigit(Mid(MyNumber, 3))
End If

ConvertHUNDREDs = Trim(Result)
End Function

' 定义子函数,转换十位数

Private Function ConvertTens(ByVal MyTens)
Dim Result As String

' 判断数字是否在 10 - 19 之间?
If Val(Left(MyTens, 1)) = 1 Then
Select Case Val(MyTens)
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
' .. 否则,它是介于 20 - 99 之间.
Select Case Val(Left(MyTens, 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

' 转换其中的个位数.
If Val(Right(MyTens, 1)) = 0 Then
Result = Result & " " & ConvertDigit(Right(MyTens, 1))
Else
Result = Result & "-" & ConvertDigit(Right(MyTens, 1))
End If
End If

ConvertTens = Result
End Function

' 定义子函数,转换个位数

Private Function ConvertDigit(ByVal MyDigit)
Select Case Val(MyDigit)
Case 1: ConvertDigit = "ONE"
Case 2: ConvertDigit = "TWO"
Case 3: ConvertDigit = "THREE"
Case 4: ConvertDigit = "FOUR"
Case 5: ConvertDigit = "FIVE"
Case 6: ConvertDigit = "SIX"
Case 7: ConvertDigit = "SEVEN"
Case 8: ConvertDigit = "EIGHT"
Case 9: ConvertDigit = "NINE"
Case Else: ConvertDigit = ""
End Select
End Function

' 定义子函数,转换小数部分

Private Function ConvertDecimal(ByVal MyDecimal)
Select Case Val(MyDecimal)
Case 1: ConvertDecimal = "ONE"
Case 2: ConvertDecimal = "TWO"
Case 3: ConvertDecimal = "THREE"
Case 4: Convert

Decimal = "FOUR"
Case 5: ConvertDecimal = "FIVE"
Case 6: ConvertDecimal = "SIX"
Case 7: ConvertDecimal = "SEVEN"
Case 8: ConvertDecimal = "EIGHT"
Case 9: ConvertDecimal = "NINE"
Case Else: ConvertDecimal = "ZERO"
End Select
End Function

Sub English()
MsgBox NumbToEnglish(123.31)
End Sub

相关主题