excel vba实例003 如何将小写金额转换成大写

 时间:2024-10-13 08:36:49

1、 为了方便演示,先做两列,一列放小写金额,一列放大写金额。数据的话,各种情况都要考虑到,整数、分为零、角为零、元为零等各种情况。

excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写
excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写

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

excel vba实例003 如何将小写金额转换成大写

8、鉴于新发现的问题:带小数的负值出错。代码修改如下,可全部复制粘贴:Public Function jedx烫喇霰嘴(curmoney As Range) As StringDim curmoney1 As LongDim i1 As LongDim i2 As IntegerDim i3 As IntegerDim s1 As String, s2 As String, s3 As StringIf curmoney = "" Thenjedx = ""Exit FunctionEnd Ifcurmoney1 = Round(curmoney * 100)If curmoney1 < 0 Thencurmoney1 = -curmoney1i1 = Int(curmoney1 / 100)i2 = Int(curmoney1 / 10) - i1 * 10i3 = curmoney1 - i1 * 100 - i2 * 10s1 = Application.WorksheetFunction.Text(i1, "[dbnum2]")s2 = Application.WorksheetFunction.Text(i2, "[dbnum2]")s3 = Application.WorksheetFunction.Text(i3, "[dbnum2]")s1 = s1 & "元"If i3 <> 0 And i2 <> 0 Thens1 = s1 & s2 & "角" & s3 & "分"If i1 = 0 Thens1 = s2 & "角" & s3 & "分"End IfEnd IfIf i3 = 0 And i2 <> 0 Thens1 = s1 & s2 & "角整"If i1 = 0 Thens1 = s2 & "角整"End IfEnd IfIf i3 <> 0 And i2 = 0 Thens1 = s1 & s2 & s3 & "分"If i1 = 0 Thens1 = s3 & "分"End IfEnd IfIf Right(s1, 1) = "元" Then s1 = s1 & "整"jedx = "负" & s1Elsei1 = Int(curmoney1 / 100)i2 = Int(curmoney1 / 10) - i1 * 10i3 = curmoney1 - i1 * 100 - i2 * 10s1 = Application.WorksheetFunction.Text(i1, "[dbnum2]")s2 = Application.WorksheetFunction.Text(i2, "[dbnum2]")s3 = Application.WorksheetFunction.Text(i3, "[dbnum2]")s1 = s1 & "元"If i3 <> 0 And i2 <> 0 Thens1 = s1 & s2 & "角" & s3 & "分"If i1 = 0 Thens1 = s2 & "角" & s3 & "分"End IfEnd IfIf i3 = 0 And i2 <> 0 Thens1 = s1 & s2 & "角整"If i1 = 0 Thens1 = s2 & "角整"End IfEnd IfIf i3 <> 0 And i2 = 0 Thens1 = s1 & s2 & s3 & "分"If i1 = 0 Thens1 = s3 & "分"End IfEnd IfIf Right(s1, 1) = "元" Then s1 = s1 & "整"jedx = s1End IfEnd Function

  • 怎样使用VBA人民币大写?
  • VBA定制之数字转人民币大写
  • excel中使用vba提取所有工作薄名称的方法
  • 怎么用VBA宏代码将单元格设为人民币大写
  • 如何利用VBA代码更改图表颜色
  • 热门搜索
    文明手抄报内容 天安城门手抄报 爱祖国手抄报内容30字 清明节手抄报第一名 关于古诗的手抄报 世界无烟日手抄报 生物手抄报 禁毒手抄报图片 春节手抄报内容文字 黄河手抄报