WORD数字小写金额变大写

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Sub 小写金额变大写()

Dim Numeric As Currency, IntPart As Long, DecimalPart As Byte, MyField As Field, Lable As String

Dim Jiao As Byte, Fen As Byte, Oddment As String, Odd As String, MyChinese As String

Const ZWDX As String = "壹贰叁肆伍陆柒捌玖零" '定义一个中文大写汉字常量

On Error Resume Next '错误忽略

With Selection

Numeric = VBA.Round(VBA.V al(.Text), 2) '四舍五入保留小数点后两位

'判断是否在表格中!!此处为向左MoveLeft移动1(count)个单元格(wdCell)

If .Information(wdWithInTable) Then _

.MoveLeft unit:=wdCell, Count:=1 Else .MoveRight unit:=wdCharacter

'对数据进行判断,是否在指定的范围内

If VBA.Abs(Numeric) > 2147483647 Then MsgBox "数值超过范围!", _

vbOKOnly + vbExclamation, "Warning": Exit Sub

IntPart = Int(VBA.Abs(Numeric)) '定义一个正整数

Odd = VBA.IIf(IntPart = 0, "", "元") '定义一个STRING变量

'插入中文大写前的标签

Lable = VBA.IIf(Numeric = VBA.Abs(Numeric), "人民币大写: ", "金额大写: 负")

'对小数点后面二位数进行择定

DecimalPart = (VBA.Abs(Numeric) - IntPart) * 100

Select Case DecimalPart

Case Is = 0 '如果是0,即是选定的数据为整数

Oddment = VBA.IIf(Odd = "", "", Odd & "整")

Case Is < 10 '<10,即是零头是分

Oddment = VBA.IIf(Odd <> "", "元零" & VBA.Mid(ZWDX, DecimalPart, 1) & "分", _

VBA.Mid(ZWDX, DecimalPart, 1) & "分")

Case 10, 20, 30, 40, 50, 60, 70, 80, 90 '如果是角整

Oddment = "元" & VBA.Mid(ZWDX, DecimalPart / 10, 1) & "角整"

Case Else '既有角,又有分的情况

Jiao = VBA.Left(CStr(DecimalPart), 1) '取得角面值

Fen = VBA.Right(CStr(DecimalPart), 1) '取得分面值

Oddment = Odd & VBA.Mid(ZWDX, Jiao, 1) & "角" '转换为角的中文大写

Oddment = Oddment & VBA.Mid(ZWDX, Fen, 1) & "分" '转换为分的中文大写

End Select

'指定区域插入中文大写格式的域

Set MyField = .Fields.Add(Range:=.Range, Text:="= " & IntPart & " \*CHINESENUM2") MyField.Select '选定域(最后是用指定文本覆盖选定区域)

'如果仅有角分情况下,Mychinese为""

MyChinese = VBA.IIf(MyField.RESULT <> "零", MyField.RESULT, "")

.Text = Lable & MyChinese & Oddment

End With

End Sub

相关文档
最新文档