ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 164|回复: 6

[讨论] 如何把整数转换为英文表述?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-10-2 09:19 | 显示全部楼层 |阅读模式
如题,

附图是例子

image.png

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-10-2 09:22 | 显示全部楼层
提供2个数组,免得写起来麻烦。
s1 = Split("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen", ",")

s2 = Split(",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety", ",")

TA的精华主题

TA的得分主题

发表于 2024-10-2 09:30 | 显示全部楼层
基础代码来自互联网,版权归原作者

  1. Option Explicit
  2. 'Main Function
  3. Function SpellNumber(ByVal MyNumber)
  4.     Dim Dollars, Cents, Temp
  5.     Dim DecimalPlace, Count
  6.     ReDim Place(9) As String
  7.     Place(2) = " Thousand "
  8.     Place(3) = " Million "
  9.     Place(4) = " Billion "
  10.     Place(5) = " Trillion "
  11.     ' String representation of amount.
  12.     MyNumber = Trim(Str(MyNumber))
  13.     ' Position of decimal place 0 if none.
  14.     DecimalPlace = InStr(MyNumber, ".")
  15.     ' Convert cents and set MyNumber to dollar amount.
  16.     If DecimalPlace > 0 Then
  17.         Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
  18.         MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  19.     End If
  20.     Count = 1
  21.     Do While MyNumber <> ""
  22.         Temp = GetHundreds(Right(MyNumber, 3))
  23.         If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
  24.         If Len(MyNumber) > 3 Then
  25.             MyNumber = Left(MyNumber, Len(MyNumber) - 3)
  26.         Else
  27.             MyNumber = ""
  28.         End If
  29.         Count = Count + 1
  30.     Loop
  31. '    Select Case Dollars
  32. '    Case ""
  33. '        Dollars = "No Dollars"
  34. '    Case "One"
  35. '        Dollars = "One Dollar"
  36. '    Case Else
  37. '        Dollars = Dollars & " Dollars"
  38. '    End Select
  39. '    Select Case Cents
  40. '    Case ""
  41. '        Cents = " and No Cents"
  42. '    Case "One"
  43. '        Cents = " and One Cent"
  44. '    Case Else
  45. '        Cents = " and " & Cents & " Cents"
  46. '    End Select
  47.     SpellNumber = Dollars & Cents
  48.     If Len(SpellNumber) = 0 Then SpellNumber = "Zero"
  49. End Function
  50. ' Converts a number from 100-999 into text
  51. Function GetHundreds(ByVal MyNumber)
  52.     Dim Result As String
  53.     If Val(MyNumber) = 0 Then Exit Function
  54.     MyNumber = Right("000" & MyNumber, 3)
  55.     ' Convert the hundreds place.
  56.     If Mid(MyNumber, 1, 1) <> "0" Then
  57.         Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
  58.     End If
  59.     ' Convert the tens and ones place.
  60.     If Mid(MyNumber, 2, 1) <> "0" Then
  61.         Result = Result & GetTens(Mid(MyNumber, 2))
  62.     Else
  63.         Result = Result & GetDigit(Mid(MyNumber, 3))
  64.     End If
  65.     GetHundreds = Result
  66. End Function
  67. ' Converts a number from 10 to 99 into text.
  68. Function GetTens(TensText)
  69.     Dim Result As String
  70.     Result = "" ' Null out the temporary function value.
  71.     If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
  72.         Select Case Val(TensText)
  73.         Case 10: Result = "Ten"
  74.         Case 11: Result = "Eleven"
  75.         Case 12: Result = "Twelve"
  76.         Case 13: Result = "Thirteen"
  77.         Case 14: Result = "Fourteen"
  78.         Case 15: Result = "Fifteen"
  79.         Case 16: Result = "Sixteen"
  80.         Case 17: Result = "Seventeen"
  81.         Case 18: Result = "Eighteen"
  82.         Case 19: Result = "Nineteen"
  83.         Case Else
  84.         End Select
  85.     Else ' If value between 20-99...
  86.         Select Case Val(Left(TensText, 1))
  87.         Case 2: Result = "Twenty "
  88.         Case 3: Result = "Thirty "
  89.         Case 4: Result = "Forty "
  90.         Case 5: Result = "Fifty "
  91.         Case 6: Result = "Sixty "
  92.         Case 7: Result = "Seventy "
  93.         Case 8: Result = "Eighty "
  94.         Case 9: Result = "Ninety "
  95.         Case Else
  96.         End Select
  97.         Result = Result & GetDigit(Right(TensText, 1))  ' Retrieve ones place.
  98.     End If
  99.     GetTens = Result
  100. End Function
  101. ' Converts a number from 1 to 9 into text.
  102. Function GetDigit(Digit)
  103.     Select Case Val(Digit)
  104.     Case 1: GetDigit = "One"
  105.     Case 2: GetDigit = "Two"
  106.     Case 3: GetDigit = "Three"
  107.     Case 4: GetDigit = "Four"
  108.     Case 5: GetDigit = "Five"
  109.     Case 6: GetDigit = "Six"
  110.     Case 7: GetDigit = "Seven"
  111.     Case 8: GetDigit = "Eight"
  112.     Case 9: GetDigit = "Nine"
  113.     Case Else: GetDigit = ""
  114.     End Select
  115. End Function

复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-10-2 09:30 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2024-10-2 10:22 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
以下代码来自于百度ai,版权归于ai!
  1. Function NumberToWords(ByVal n As Long) As String
  2.     Dim units() As String
  3.     Dim lessThan20() As String
  4.     Dim tens() As String
  5.     Dim result As String
  6.     Dim i As Long
  7.     Dim billions As Long, millions As Long, thousands As Long
  8.     Dim hundreds As Long, remainder As Long
  9.    
  10.     ' 定义英文数字单位
  11.     lessThan20 = Split("Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Eleven,Twelve,Thirteen,Fourteen,Fifteen,Sixteen,Seventeen,Eighteen,Nineteen", ",")
  12.     tens = Split(",Ten,Twenty,Thirty,Forty,Fifty,Sixty,Seventy,Eighty,Ninety", ",")
  13.    
  14.     If n = 0 Then
  15.         NumberToWords = "Zero"
  16.         Exit Function
  17.     End If
  18.    
  19.     ' 处理大于19的数字
  20.     If n < 0 Then
  21.         NumberToWords = "Negative " & NumberToWords(-n)
  22.         Exit Function
  23.     End If
  24.    
  25.     billions = n \ 1000000000
  26.     millions = (n Mod 1000000000) \ 1000000
  27.     thousands = (n Mod 1000000) \ 1000
  28.     hundreds = (n Mod 1000) \ 100
  29.     remainder = n Mod 100
  30.    
  31.     If billions > 0 Then result = result & NumberToWords(billions) & " Billion "
  32.     If millions > 0 Then result = result & NumberToWords(millions) & " Million "
  33.     If thousands > 0 Then result = result & NumberToWords(thousands) & " Thousand "
  34.     If hundreds > 0 Then result = result & NumberToWords(hundreds) & " Hundred "
  35.    
  36.     If remainder < 20 And remainder > 0 Then
  37.         result = result & lessThan20(remainder)
  38.     ElseIf remainder >= 20 Then
  39.         result = result & tens(remainder \ 10)
  40.         If remainder Mod 10 > 0 Then result = result & "-" & lessThan20(remainder Mod 10)
  41.     End If
  42.    
  43.     ' 去除尾部多余的空格
  44.     NumberToWords = Trim(result)
  45. End Function

复制代码

TA的精华主题

TA的得分主题

发表于 2024-10-2 11:24 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
供参考...
2024-10-02_112236.jpg

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2024-10-2 14:51 来自手机 | 显示全部楼层
https://club.excelhome.net/forum.php?mod=viewthread&tid=1667380&
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-19 05:50 , Processed in 0.039879 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表