|
本帖最后由 sinc4233 于 2020-5-16 10:46 编辑
- Sub 人民币中文大写()
- '功能:全文查找数字元(也可单选数字)转换为人民币中文大写
- Dim i As String, j As String, k As String, x As String, y As String, u As Long, v As Long, n As Long, s As Long
- x = MsgBox("大写是否放在数字前面?(否则后面)", vbYesNoCancel + vbExclamation, "人民币中文大写")
- If x = vbYes Then
- n = 1
- ElseIf x = vbNo Then
- n = 0
- Else
- End
- End If
- '全文/单个
- If Selection.Type <> wdSelectionIP Then
- Do While Selection.Characters.First Like "[!0-90-9]"
- Selection.MoveStart unit:=wdCharacter, Count:=1
- Loop
- Do While Not (Selection.Characters.Last Like "[0-90-9.,, ]" Or Selection.Characters.Last Like ChrW(160))
- Selection.MoveEnd unit:=wdCharacter, Count:=-1
- Loop
- Selection.MoveEnd unit:=wdCharacter, Count:=1
- If Selection Like "*[!元]" Then
- Selection.MoveEnd unit:=wdCharacter, Count:=-1
- Selection.InsertAfter Text:="元"
- End If
- s = 1
- GoTo SingleNum
- End If
- '全文查找数字元
- Selection.HomeKey unit:=wdStory
- Selection.Find.ClearFormatting
- Do While Selection.Find.Execute(findtext:="^#", Forward:=True, MatchWildcards:=False)
- Do While Selection.Characters.Last Like "[0-90-9.,, ]" Or Selection.Characters.Last Like ChrW(160)
- Selection.MoveEnd unit:=wdCharacter, Count:=1
- Loop
- SingleNum:
- If Selection Like "*元" Then
- '格式处理
- Selection.Font.Bold = True '加粗
- Selection.Font.Color = wdColorBlue '蓝色
- Selection.Font.Underline = wdUnderlineWavyHeavy '重波浪线
- '选定数字
- Selection.MoveEnd unit:=wdCharacter, Count:=-1
- k = "万仟佰拾亿仟佰拾万仟佰拾元空角分"
- i = Selection
- '规范数字
- i = Replace(i, ChrW(160), "") '替换不间断空格
- i = Replace(i, " ", "") '替换半角空格
- i = Replace(i, " ", "") '替换全角空格
- i = Replace(i, ",", "") '替换英文逗号
- i = Replace(i, ",", "") '替换中文逗号
- i = Format(i) '删除数字前后无效零/小数点
- '单个数字
- If s = 1 Then
- i = StrConv(i, vbNarrow) '全角转半角
- Dim a
- a = Val(i) '数值型
- If Len(i) <> Len(a) Then MsgBox "非纯数字,无法转换!!!", vbOKOnly + vbCritical, "人民币中文大写": End
- End If
- If i Like ".*" Then i = "0" & i
- '错误报警
- If Selection Like "*.*.*" Or Val(i) > 999999999999.99 Then
- Selection.Font.Color = wdColorRed '红色
- Selection.Font.Underline = wdUnderlineWavyHeavy '重波浪线
- GoTo Skip
- End If
- '强制格式
- If i Like "*?.??" Then
- ElseIf i Like "*?.?" Then
- i = i & "0"
- ElseIf i Like "*?.???*" Then
- i = Format(i, "0.00")
- Else
- i = i & ".00"
- End If
- '核心代码
- v = Len(i)
- k = Right(k, v)
- u = 0
- y = ""
- Do
- u = u + 1
- j = Mid(i, u, 1) & Mid(k, u, 1)
- y = y & j
- Loop Until u = v
- i = y
- '基本替换
- i = Replace(i, "1", "壹")
- i = Replace(i, "2", "贰")
- i = Replace(i, "3", "叁")
- i = Replace(i, "4", "肆")
- i = Replace(i, "5", "伍")
- i = Replace(i, "6", "陆")
- i = Replace(i, "7", "柒")
- i = Replace(i, "8", "捌")
- i = Replace(i, "9", "玖")
- i = Replace(i, "0", "零")
- i = Replace(i, ".", "点")
- i = Replace(i, "点空", "")
- '零字替换
- i = Replace(i, "零仟", "零")
- i = Replace(i, "零佰", "零")
- i = Replace(i, "零拾", "零")
- Do While i Like "*零零*"
- i = Replace(i, "零零", "零")
- Loop
- i = Replace(i, "零亿", "亿零")
- i = Replace(i, "零万", "万零")
- i = Replace(i, "零元", "元零")
- Do While i Like "*零零*"
- i = Replace(i, "零零", "零")
- Loop
- i = Replace(i, "零角零分", "整")
- i = Replace(i, "零角", "零")
- i = Replace(i, "零分", "")
- If i Like "元零*" Then i = Replace(i, "元零", "")
- If i = "元整" Then i = "零" & i
- If i Like "*万零元*" And Val(i) < 100000000 Then i = Replace(i, "万零元", "万元") '10万<=X<1亿
- i = Replace(i, "亿万", "亿")
- If i Like "*亿零万*" Then i = Replace(i, "零万", "") '>=10亿
- '转换结果
- i = "(人民币" & i & ")"
- If n = 1 Then Selection.InsertBefore Text:=i Else Selection.MoveEnd unit:=wdCharacter, Count:=1: Selection.InsertAfter Text:=i
- If s = 1 Then End
- End If
- Skip:
- Selection.MoveRight unit:=wdCharacter, Count:=1
- Loop
- End Sub
复制代码
楼主你好 , 今天翻了半天找到你的这个代码.还可以用.就是和我想要的结果还有点不一样,由于不懂代码.改了半天只实现一点点自己想要的结果.麻烦楼主帮忙改下吗?
如果自己选中数字后,替换的结果不要原数字+ 元 + 大写, 只要大写的结果就可以了.然后全文替换的时候只改 "金额大写:" 后面的数字. 麻烦楼主方便的时候帮忙改下.非常感谢.
代码原结果:
想要的结果:
|
|