ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 如何将数字转化为英文格式?

如何将数字转化为英文格式?

作者:绿色风 分类: 时间:2022-08-18 浏览:68
楼主
99253415
Q:如何将数字转化为英文格式?

 
A:
  1. Function SpellNumber(ByVal MyNumber)
  2. Dim Dollars, Cents, Temp
  3. Dim DecimalPlace, Count
  4. ReDim Place(9) As String
  5. Application.Volatile True
  6. Place(2) = " Thousand "
  7. Place(3) = " Million "
  8. Place(4) = " Billion "
  9. Place(5) = " Trillion " ' String representation of amount
  10. MyNumber = Trim(Str(MyNumber)) ' Position of decimal place 0 if none
  11. DecimalPlace = InStr(MyNumber, ".")
  12. 'Convert cents and set MyNumber to dollar amount
  13. If DecimalPlace > 0 Then
  14. Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
  15. MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
  16. End If
  17. Count = 1
  18. Do While MyNumber <> ""
  19. Temp = GetHundreds(Right(MyNumber, 3))
  20. If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
  21. If Len(MyNumber) > 3 Then
  22. MyNumber = Left(MyNumber, Len(MyNumber) - 3)
  23. Else
  24. MyNumber = ""
  25. End If
  26. Count = Count + 1
  27. Loop
  28. Select Case Dollars
  29. Case ""
  30. Dollars = "No Dollars"
  31. Case "One"
  32. Dollars = "One Dollar"
  33. Case Else
  34. Dollars = Dollars & " Dollars"
  35. End Select
  36. Select Case Cents
  37. Case ""
  38. Cents = " and No Cents"
  39. Case "One"
  40. Cents = " and One Cent"
  41. Case Else
  42. Cents = " and " & Cents & " Cents"
  43. End Select
  44. SpellNumber = Dollars & Cents
  45. End Function
  1. 'Converts a number from 100-999 into text
  2. Function GetHundreds(ByVal MyNumber)
  3. Dim Result As String
  4. If Val(MyNumber) = 0 Then Exit Function
  5. MyNumber = Right("000" & MyNumber, 3) 'Convert the hundreds place
  6. If Mid(MyNumber, 1, 1) <> "0" Then
  7. Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
  8. End If
  9. 'Convert the tens and ones place
  10. If Mid(MyNumber, 2, 1) <> "0" Then
  11. Result = Result & GetTens(Mid(MyNumber, 2))
  12. Else
  13. Result = Result & GetDigit(Mid(MyNumber, 3))
  14. End If
  15. GetHundreds = Result
  16. End Function
  17. ' Converts a number from 10 to 99 into text.
  18. Function GetTens(TensText)
  19. Dim Result As String
  20. Result = "" 'null out the temporary function value
  21. If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19
  22. Select Case Val(TensText)
  23. Case 10: Result = "Ten"
  24. Case 11: Result = "Eleven"
  25. Case 12: Result = "Twelve"
  26. Case 13: Result = "Thirteen"
  27. Case 14: Result = "Fourteen"
  28. Case 15: Result = "Fifteen"
  29. Case 16: Result = "Sixteen"
  30. Case 17: Result = "Seventeen"
  31. Case 18: Result = "Eighteen"
  32. Case 19: Result = "Nineteen"
  33. Case Else
  34. End Select
  35. Else ' If value between 20-99
  36. Select Case Val(Left(TensText, 1))
  37. Case 2: Result = "Twenty "
  38. Case 3: Result = "Thirty "
  39. Case 4: Result = "Forty "
  40. Case 5: Result = "Fifty "
  41. Case 6: Result = "Sixty "
  42. Case 7: Result = "Seventy "
  43. Case 8: Result = "Eighty "
  44. Case 9: Result = "Ninety "
  45. Case Else
  46. End Select
  47. Result = Result & GetDigit _
  48. (Right(TensText, 1)) 'Retrieve ones place
  49. End If
  50. GetTens = Result
  51. End Function
  52. ' Converts a number from 1 to 9 into text.
  53. Function GetDigit(Digit)
  54. Select Case Val(Digit)
  55. Case 1: GetDigit = "One"
  56. Case 2: GetDigit = "Two"
  57. Case 3: GetDigit = "Three"
  58. Case 4: GetDigit = "Four"
  59. Case 5: GetDigit = "Five"
  60. Case 6: GetDigit = "Six"
  61. Case 7: GetDigit = "Seven"
  62. Case 8: GetDigit = "Eight"
  63. Case 9: GetDigit = "Nine"
  64. Case Else: GetDigit = ""
  65. End Select
  66. End Function

该帖已经同步到

英文数字.rar
2楼
天南地北
微软自带的帮助,O(∩_∩)O哈哈~!
3楼
纵鹤擒龙水中月
这个收藏留用

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap