ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 【求助!】excel中如何把数字金额转换成英文

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-8-7 23:25 | 显示全部楼层 |阅读模式

对比图.PNG

说明:上图中,黄色内容是下面代码自动转换的效果,请大神帮忙把代码改一下,让它自动翻译成原版那样的格式。即把Say Total HKD加在最前面,同时去掉Dollars英文就可以了。小弟在此多谢!


代码如下:
Function SS(ByVal pNumber)
Dim Dollars, Only
arr = Array("", "", " Thousand ", " Million ", " Billion ", " Trillion ")
pNumber = Trim(Str(pNumber))
xDecimal = InStr(pNumber, ".")
If xDecimal > 0 Then
Only = GetTens(Left(Mid(pNumber, xDecimal + 1) & "00", 2))
pNumber = Trim(Left(pNumber, xDecimal - 1))
End If
xIndex = 1
Do While pNumber <> ""
xHundred = ""
xValue = Right(pNumber, 3)
If Val(xValue) <> 0 Then
xValue = Right("000" & xValue, 3)
If Mid(xValue, 1, 1) <> "0" Then
xHundred = GetDigit(Mid(xValue, 1, 1)) & " Hundred "
End If
If Mid(xValue, 2, 1) <> "0" Then
xHundred = xHundred & GetTens(Mid(xValue, 2))
Else
xHundred = xHundred & GetDigit(Mid(xValue, 3))
End If
End If
If xHundred <> "" Then
Dollars = xHundred & arr(xIndex) & Dollars
End If
If Len(pNumber) > 3 Then
pNumber = Left(pNumber, Len(pNumber) - 3)
Else
pNumber = ""
End If
xIndex = xIndex + 1
Loop
Select Case Dollars
Case ""
Dollars = "No Dollars"
Case "One"
Dollars = "One Dollar"
Case Else
Dollars = Dollars & " Dollars"
End Select
Select Case Only
Case ""
Only = " and Cents No Only"
Case "One"
Only = " and Cents One Cent"
Case Else
Only = " and Cents " & Only & " Only"
End Select
SS = Dollars & Only
End Function
Function GetTens(pTens)
Dim Result As String
Result = ""
If Val(Left(pTens, 1)) = 1 Then
Select Case Val(pTens)
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
Select Case Val(Left(pTens, 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 & GetDigit(Right(pTens, 1))
End If
GetTens = Result
End Function
Function GetDigit(pDigit)
Select Case Val(pDigit)
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



TA的精华主题

TA的得分主题

发表于 2018-8-7 23:55 来自手机 | 显示全部楼层
查找替换 replace 不就搞定了吗?

TA的精华主题

TA的得分主题

发表于 2018-8-8 08:44 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
本帖最后由 zopey 于 2018-8-8 08:46 编辑

添加到最后
SS = “Say Total HKD ” & replace(SS,"Dollars ","")

TA的精华主题

TA的得分主题

发表于 2018-8-8 09:45 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
'==================== money change to English format==============  '****************' Main Function *'**************** Function SpellNumber(ByVal MyNumber)     Dim Dollars, Cents, Temp     Dim DecimalPlace, count     Dim place     ReDim place(9) As String     Application.Volatile True     place(2) = " Thousand "     place(3) = " Million "     place(4) = " Billion "     place(5) = " Trillion "     ' String representation of amount     MyNumber = VBA.Trim(VBA.Str(MyNumber))     ' Position of decimal place 0 if none     DecimalPlace = InStr(MyNumber, ".")     'Convert cents and set MyNumber to dollar amount     If DecimalPlace > 0 Then         Cents = GetTens(VBA.Left(VBA.Mid(MyNumber, DecimalPlace + 1) & "00", 2))         MyNumber = VBA.Trim(VBA.Left(MyNumber, DecimalPlace - 1))         End If     count = 1     Do While MyNumber <> ""        Temp = GetHundreds(VBA.Right(MyNumber, 3))        If Temp <> "" Then Dollars = Temp & place(count) & Dollars           If Len(MyNumber) > 3 Then              MyNumber = VBA.Left(MyNumber, Len(MyNumber) - 3)              Else             MyNumber = ""             End If             count = count + 1             Loop     Select Case Dollars         Case ""             Dollars = "No Dollars"         Case "One"             Dollars = "One Dollar"         Case Else             Dollars = Dollars & " Dollars"     End Select     Select Case Cents         Case ""             Cents = " and No Cents"         Case "One"             Cents = " and One Cent"         Case Else             Cents = " and " & Cents & "Cents"     End Select     SpellNumber = Dollars & Cents     End Function '******************************************* ' Converts a number from 100-999 into text * '******************************************* Function GetHundreds(ByVal MyNumber)     Dim Result As String     If val(MyNumber) = 0 Then Exit Function     MyNumber = VBA.Right("000" & MyNumber, 3)     'Convert the hundreds place     If VBA.Mid(MyNumber, 1, 1) <> "0" Then         Result = GetDigit(VBA.Mid(MyNumber, 1, 1)) & " Hundred "         End If     'Convert the tens and ones place     If VBA.Mid(MyNumber, 2, 1) <> "0" Then         Result = Result & GetTens(VBA.Mid(MyNumber, 2))         Else         Result = Result & GetDigit(VBA.Mid(MyNumber, 3))         End If     GetHundreds = Result     End Function '********************************************* ' Converts a number from 10 to 99 into text. * '********************************************* Function GetTens(TensText)     Dim Result As String     Result = ""           'null out the temporary function value     If val(VBA.Left(TensText, 1)) = 1 Then   ' If value between 10-19         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                                 ' If value between 20-99         Select Case val(VBA.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 & GetDigit _             (VBA.Right(TensText, 1))  'Retrieve ones place             End If       GetTens = Result       End Function '******************************************* ' Converts a number from 1 to 9 into text. * '******************************************* Function GetDigit(Digit)     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  

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-8 19:52 来自手机 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-9 22:46 来自手机 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
楼上说的,能看懂的我都试了,还是不行。求高手帮忙,谢谢

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-9 22:58 | 显示全部楼层
duquancai 发表于 2018-8-7 23:55
查找替换 replace 不就搞定了吗?

大神,能说再具体一点吗,因为我是小白

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-9 23:00 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
zopey 发表于 2018-8-8 08:44
添加到最后
SS = “Say Total HKD ” & replace(SS,"Dollars ","")

Only = " and Cents " & Only & " Only"
End Select
SS = "Say Total HKD" & Replace(SS, "Dollars", "")
End Function

大神,是不是这样改,按以上现在涵数无法代出了。显示#NAME?

TA的精华主题

TA的得分主题

发表于 2018-8-10 08:21 | 显示全部楼层
Cr-sky 发表于 2018-8-9 23:00
Only = " and Cents " & Only & " Only"
End Select
SS = "Say Total HKD" & Replace(SS, "Dollars", " ...

Only = " and Cents " & Only & " Only"
End Select
SS = Dollars & Only
SS = "Say Total HKD" & Replace(SS, "Dollars", "")

评分

2

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-10 10:26 | 显示全部楼层
zopey 发表于 2018-8-10 08:21
Only = " and Cents " & Only & " Only"
End Select
SS = Dollars & Only

厉害了我的哥,成功了。非常谢谢
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2025-1-11 20:38 , Processed in 0.027516 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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