作者:绿色风
分类:
时间:2022-08-17
浏览:197
楼主 Zaezhong |
Q:如何将A列的数字转换为英文表示人民币的金额?
A:- Option Explicit
- '主要函数
- Function EngNumber(ByVal MyNumber)
- Dim RMB, Yuans, Temp
- Dim DecimalPlace, Count
- ReDim Place(9) As String
- Place(2) = " Thousand "
- Place(3) = " Million "
- Place(4) = " Billion "
- Place(5) = " Trillion "
- ' 将数字Mynumber转换成字符串格式,并去掉多余空格
- MyNumber = Trim(Str(MyNumber))
- '查找小数点的位置
- DecimalPlace = InStr(MyNumber, ".")
- '如果存在小数点
- If DecimalPlace > 0 Then
- Yuans = EngTens(Left(Mid(MyNumber, DecimalPlace + 1) & _
- "00", 2))
- ' 去掉小数部分,保留剩下的整数部分留做转换
- MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
- End If
- Count = 1
- Do While MyNumber <> ""
- ' 将最后的三位数字转换成英文
- Temp = EngHundreds(Right(MyNumber, 3))
- If Temp <> "" Then RMB = Temp & Place(Count) & RMB
- ' 如果整数部分大于三位,再向前移动三位数字重复进行转换
- If Len(MyNumber) > 3 Then
- MyNumber = Left(MyNumber, Len(MyNumber) - 3)
- Else
- MyNumber = ""
- End If
- Count = Count + 1
- Loop
- Select Case RMB
- Case ""
- RMB = "No RMB"
- Case "One"
- RMB = "One Yuan"
- Case Else
- RMB = RMB & " RMB"
- End Select
- Select Case Yuans
- Case ""
- Yuans = " and No Yuans"
- Case "One"
- Yuans = " and One Yuan"
- Case Else
- Yuans = " and " & Yuans & " Fen"
- End Select
- EngNumber = RMB & Yuans
- End Function
- '定义子函数,转换百位
- Function EngHundreds(ByVal MyNumber)
- Dim Result As String
- '如果为空就退出
- If Val(MyNumber) = 0 Then Exit Function
- '如果不满3位在前面补0
- MyNumber = Right("000" & MyNumber, 3)
- ' 判断是否有百位数可供转换
- If Mid(MyNumber, 1, 1) <> "0" Then
- Result = EngDigit(Mid(MyNumber, 1, 1)) & " Hundred "
- End If
- If Mid(MyNumber, 2, 1) <> "0" Then
- Result = Result & EngTens(Mid(MyNumber, 2))
- Else
- ' 如果没有,转换个位数
- Result = Result & EngDigit(Mid(MyNumber, 3))
- End If
- EngHundreds = Result
- End Function
-
- '定义子函数,转换十位
- Function EngTens(TensText)
- Dim Result As String
- Result = ""
- ' 判断数字是否在 10 - 19 之间
- If Val(Left(TensText, 1)) = 1 Then
- Select 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 Else
- End Select
- Else
- '否则在 20 - 99 之间
- Select 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 Else
- End Select
- Result = Result & EngDigit _
- (Right(TensText, 1))
- End If
- EngTens = Result
- End Function
-
- '定义子函数,转换个位
- Function EngDigit(Digit)
- Select Case Val(Digit)
- Case 1: EngDigit = "One"
- Case 2: EngDigit = "Two"
- Case 3: EngDigit = "Three"
- Case 4: EngDigit = "Four"
- Case 5: EngDigit = "Five"
- Case 6: EngDigit = "Six"
- Case 7: EngDigit = "Seven"
- Case 8: EngDigit = "Eight"
- Case 9: EngDigit = "Nine"
- Case Else: EngDigit = ""
- End Select
- End Function
数字转换.rar |
2楼 bishunbiao |
下载学习 |
3楼 cyy1905 |
有没有用函数简单点的呢?? |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一