excel vba实例003 如何将小写金额转换成大写
来源:网络收集 点击: 时间:2024-04-07为了方便演示,先做两列,一列放小写金额,一列放大写金额。数据的话,各种情况都要考虑到,整数、分为零、角为零、元为零等各种情况。

按Alt+F11,打开VBA编辑窗口,载入图空白位置右键——插入——模块,然后再双击出现的模块就可以进行编辑了。

【注:最新修改代码在最后一步。】
在模块编辑窗口中输入如下代码(由于有人反馈说当单元格是空的时候显示成了零元整,这样不对,我也觉得不妥,所以在这里本着对读者负责的态度在这里修改一下,修改注释在后面):
“Public Function jedx(curmoney As Currency) As String currency改为Range
Dim curmoney1 As Long
Dim i1 As Long
Dim i2 As Integer
Dim i3 As Integer
Dim s1 As String, s2 As String, s3 As String
可以在此位置插入如下代码,单引号去掉
if curmoney= then jedx=
exit function
end if
curmoney1 = Round(curmoney * 100)
i1 = Int(curmoney1 / 100)
i2 = Int(curmoney1 / 10) - i1 * 10
i3 = curmoney1 - i1 * 100 - i2 * 10
s1 = Application.WorksheetFunction.Text(i1, )
s2 = Application.WorksheetFunction.Text(i2, )
s3 = Application.WorksheetFunction.Text(i3, )
s1 = s1 元
If i3 0 And i2 0 Then
s1 = s1 s2 角 s3 分
If i1 = 0 Then
s1 = s2 角 s3 分
End If
End If
If i3 = 0 And i2 0 Then
s1 = s1 s2 角整
If i1 = 0 Then
s1 = s2 角整
End If
End If
If i3 0 And i2 = 0 Then
s1 = s1 s2 s3 分
If i1 = 0 Then
s1 = s3 分
End If
End If
If Right(s1, 1) = 元 Then s1 = s1 整
jedx = s1
End Function”
注:不包含双引号,其中jedx是金额大写的缩写,便于记忆引用。

代码写完确认无误后保存,保存是出现此对话框,选择否,另存为加载宏格式,即后缀*.xla。我这里把文件命名为“jedx金额大写.xla”。


但你需要用到的时候,就可以在编辑excel文件的时候打开之前保存的加载宏文件,我这里是保存为“jedx金额大写.xla”的文件。

打开之后再excel中就可以像使用其他函数一样,我这里是输入“=jedx(B2)”,可以看到,当你输入到一定时候excel会像提示其他函数那样,提示这个函数。

函数输入结束回车,小写的金额就变成了大写的了,下拉验证其他各式各样类型的金额,发现没有错误就OK了。

鉴于新发现的问题:带小数的负值出错。
代码修改如下,可全部复制粘贴:
Public Function jedx(curmoney As Range) As String
Dim curmoney1 As Long
Dim i1 As Long
Dim i2 As Integer
Dim i3 As Integer
Dim s1 As String, s2 As String, s3 As String
If curmoney = Then
jedx =
Exit Function
End If
curmoney1 = Round(curmoney * 100)
If curmoney1 0 Then
curmoney1 = -curmoney1
i1 = Int(curmoney1 / 100)
i2 = Int(curmoney1 / 10) - i1 * 10
i3 = curmoney1 - i1 * 100 - i2 * 10
s1 = Application.WorksheetFunction.Text(i1, )
s2 = Application.WorksheetFunction.Text(i2, )
s3 = Application.WorksheetFunction.Text(i3, )
s1 = s1 元
If i3 0 And i2 0 Then
s1 = s1 s2 角 s3 分
If i1 = 0 Then
s1 = s2 角 s3 分
End If
End If
If i3 = 0 And i2 0 Then
s1 = s1 s2 角整
If i1 = 0 Then
s1 = s2 角整
End If
End If
If i3 0 And i2 = 0 Then
s1 = s1 s2 s3 分
If i1 = 0 Then
s1 = s3 分
End If
End If
If Right(s1, 1) = 元 Then s1 = s1 整
jedx = 负 s1
Else
i1 = Int(curmoney1 / 100)
i2 = Int(curmoney1 / 10) - i1 * 10
i3 = curmoney1 - i1 * 100 - i2 * 10
s1 = Application.WorksheetFunction.Text(i1, )
s2 = Application.WorksheetFunction.Text(i2, )
s3 = Application.WorksheetFunction.Text(i3, )
s1 = s1 元
If i3 0 And i2 0 Then
s1 = s1 s2 角 s3 分
If i1 = 0 Then
s1 = s2 角 s3 分
End If
End If
If i3 = 0 And i2 0 Then
s1 = s1 s2 角整
If i1 = 0 Then
s1 = s2 角整
End If
End If
If i3 0 And i2 = 0 Then
s1 = s1 s2 s3 分
If i1 = 0 Then
s1 = s3 分
End If
End If
If Right(s1, 1) = 元 Then s1 = s1 整
jedx = s1
End If
End Function
注意事项代码比较长,如果出现错误,需要耐心对待。
注意保存格式为加载宏,即后缀为*.xla。
如果以后要用到该加载宏文件,在编辑中的excel中直接打开即可使用。
最新修改代码在最后一步,如发现新问题可以互相探讨。
小写金额转大写EXCEL金额转换金额转换的方法VBA金额转换如何转成大写版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_455522.html