我有一列数字。在下一栏中,我想要数字的文本/单词转换。
示例:
123.561
将转换为 One hundred twenty three point five six one
。
我不想转换为货币,只是将数字转换为文本,小数位数任意。
我该怎么做?
编辑:我已将以下过程改编为非货币、无限小数位。
编辑 2 考虑通过 (1) Function SpellNumber
和
(2)
Function fractionWords
中的两项更改来实现 国际化,以使代码与其他 小数分隔符 兼容(例如中欧的冒号) ' - 见评论
MsgBox SpellNumber(2123.4575)
...返回:
Two Thousand One Hundred Twenty Three point Four Five Seven Five
将以下代码粘贴到新模块中:
Option Explicit
Function SpellNumber(ByVal numIn)
Dim LSide, RSide, Temp, DecPlace, Count, oNum
oNum = numIn
ReDim Place(9) As String
Place(2) = " Thousand "
Place(3) = " Million "
Place(4) = " Billion "
Place(5) = " Trillion "
numIn = Trim(Str(numIn)) 'String representation of amount
' Edit 2.(0)/Internationalisation
' Don't change point sign here as the above assignment preserves the point!
DecPlace = InStr(numIn, ".") 'Pos of dec place 0 if none
If DecPlace > 0 Then 'Convert Right & set numIn
RSide = GetTens(Left(Mid(numIn, DecPlace + 1) & "00", 2))
numIn = Trim(Left(numIn, DecPlace - 1))
End If
RSide = numIn
Count = 1
Do While numIn <> ""
Temp = GetHundreds(Right(numIn, 3))
If Temp <> "" Then LSide = Temp & Place(Count) & LSide
If Len(numIn) > 3 Then
numIn = Left(numIn, Len(numIn) - 3)
Else
numIn = ""
End If
Count = Count + 1
Loop
SpellNumber = LSide
If InStr(oNum, Application.DecimalSeparator) > 0 Then ' << Edit 2.(1)
SpellNumber = SpellNumber & " point " & fractionWords(oNum)
End If
End Function
Function GetHundreds(ByVal numIn) 'Converts a number from 100-999 into text
Dim w As String
If Val(numIn) = 0 Then Exit Function
numIn = Right("000" & numIn, 3)
If Mid(numIn, 1, 1) <> "0" Then 'Convert hundreds place
w = GetDigit(Mid(numIn, 1, 1)) & " Hundred "
End If
If Mid(numIn, 2, 1) <> "0" Then 'Convert tens and ones place
w = w & GetTens(Mid(numIn, 2))
Else
w = w & GetDigit(Mid(numIn, 3))
End If
GetHundreds = w
End Function
Function GetTens(TensText) 'Converts a number from 10 to 99 into text
Dim w As String
w = "" 'Null out the temporary function value
If Val(Left(TensText, 1)) = 1 Then 'If value between 10-19
Select Case Val(TensText)
Case 10: w = "Ten"
Case 11: w = "Eleven"
Case 12: w = "Twelve"
Case 13: w = "Thirteen"
Case 14: w = "Fourteen"
Case 15: w = "Fifteen"
Case 16: w = "Sixteen"
Case 17: w = "Seventeen"
Case 18: w = "Eighteen"
Case 19: w = "Nineteen"
Case Else
End Select
Else 'If value between 20-99..
Select Case Val(Left(TensText, 1))
Case 2: w = "Twenty "
Case 3: w = "Thirty "
Case 4: w = "Forty "
Case 5: w = "Fifty "
Case 6: w = "Sixty "
Case 7: w = "Seventy "
Case 8: w = "Eighty "
Case 9: w = "Ninety "
Case Else
End Select
w = w & GetDigit _
(Right(TensText, 1)) 'Retrieve ones place
End If
GetTens = w
End Function
Function GetDigit(Digit) 'Converts a number from 1 to 9 into text
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
Function fractionWords(n) As String
Dim fraction As String, x As Long
fraction = Split(n, Application.DecimalSeparator)(1) ' << Edit 2.(2)
For x = 1 To Len(fraction)
If fractionWords <> "" Then fractionWords = fractionWords & " "
fractionWords = fractionWords & GetDigit(Mid(fraction, x, 1))
Next x
End Function
(改编自来源:Microsoft)
网上还有其他几个例子。如果您正在搜索“将数字转换为文本”,您可能已经找到了它们,因为这意味着更改数据类型。更好的搜索词是 "vba 将数字转换为单词"。
=TRIM(LET(U,A1,V,IF(U>999999999,RIGHT(U,9),U),A,{1,"一";2,"二";3,"三";4 ,"四";5,"五";6,"六";7,"七";8,"八";9,"九";10,"十";11,"十一";12,"十二”;13、“十三”;14、“十四”;15、“十五”;16、“十六”;17、“十七”;18、“十八”;19、“十九”;20、“二十” ;21、“二十一”;22、“二十二”;23、“二十三”;24、“二十四”;25、“二十五”;26、“二十六”;27、“二十七”; 28、“二十八”;29、“二十九”;30、“三十”;31、“三十一”;32、“三十二”;33、“三十三”;34、“三十四”;35、 “三十五”;36、“三十六”;37、“三十七”;38、“三十八”;39、“三十九”;40、“四十”;41、“四十一”;42、“四十”二”;43、“四十三”;44、“四十四”;45、“四十五”;46、“四十六”;47、“四十七”;48、“四十八”;49、“四十九” ";50,"五十";51,"五十一";52,"五十二";53,"五十三";54,"五十四";55,"五十五";56,"五十六"; 57、“五十七”;58、“五十八”;59、“五十九”;60、“六十”;61、“六十一”;62、“六十二”;63、“六十三”;64、 “六十四”;65、“六十五”;66、“六十六”;67、“六十七”;68、“六十八”;69、“六十九”;70、“七十”;71、“七十”一”;72、“七十二”;73、“七十三”;74、“七十四”;75、“七十五”;76、“七十六”;77、“七十七”;78、“七十八” ";79、"七十九";80、"八十";81、"八十一";82、"八十二";83、"八十三";84、"八十四";85、"八十五"; 86、“八十六”;87、“八十七”;88、“八十八”;89、“八十九”;90、“九十”;91、“九十一”;92、“九十二”;93、 "九十三";94,"九十四";95,"九十五";96,"九十六";97,"九十七";98,"九十八";99,"九十九"},"卢比"&IF(U>999999999,IF(ROUNDDOWN(LEFT(U,LEN(U)-9)*100/10000000,0)=0,"",VLOOKUP(ROUNDDOWN(LEFT(U,LEN(U)-9)) *100/10000000,0)+0,A,2,0)&" Cr ")&IF(右(向下舍入(左(U,LEN(U)-9))*100/100000,0),2)+0 =0,"",VLOOKUP(RIGHT(ROUNDDOWN(LEFT(U,LEN(U)-9)*100/100000,0),2)+0,A,2,0)&"十万")&IF(RIGHT (向下舍入(左(U,LEN(U)-9)*100/1000,0),2)+0=0,"",VLOOKUP(右(向下舍入(左(U,LEN(U)-9))* 100/1000,0),2)+0,A,2,0)&"千")&IF(RIGHT(ROUNDDOWN(LEFT(U,LEN(U)-9)*100/100,0),1) +0=0,"",VLOOKUP(RIGHT(ROUNDDOWN(LEFT(U,LEN(U)-9)*100/100,0),1)+0,A,2,0)&"百")&IF (右(圆(左(U,LEN(U)-9)*100,0),2)+0=0,"",VLOOKUP(右(圆(左(U,LEN(U)-9))* 100,0),2)+0,A,2,0)),"")&IF(ROUNDDOWN(V/10000000,0)=0,"",VLOOKUP(ROUNDDOWN(V/10000000,0)+0, A,2,0)&" Cr ")&IF(右(下舍入(V/100000,0),2)+0=0,"",VLOOKUP(右(下舍入(V/100000,0),2)+ 0,A,2,0)&"十万")&IF(RIGHT(ROUNDDOWN(V/1000,0),2)+0=0,"",VLOOKUP(RIGHT(ROUNDDOWN(V/1000,0),2 )+0,A,2,0)&"千位")&IF(RIGHT(ROUNDDOWN(V/100,0),1)+0=0,"",VLOOKUP(RIGHT(ROUNDDOWN(V/100,0)) ,1)+0,A,2,0)&"百")&IF(RIGHT(ROUND(V,0),2)+0=0,"",VLOOKUP(RIGHT(ROUND(V,0),2 )+0,A,2,0))&“仅”))