本帖最后由 413191246se 于 2020-12-20 01:36 编辑
* 前几天,在回复坛友《中华人民共和国民法典》的过程中,用我的《第一章》宏排版发现最后 Word 2019 出现“内存溢出”提示,因为该《法典》有 1260 条,可能是其中的域“CHINESENUM2”或“CHINESENUM3”出错,它无法处理这么多条,将阿拉伯数字转换为汉字数目字(比如将“260”转换为“二百六十”),也许是流程导致的内存紧张,不得而知!但不管是不是这个原因,我要抛弃“CHINESENUM2/3”域,改用“直译法”将数字转为汉字。
* 功能:将自然数(小于等于9999)转换为汉字数目字(比如将“1260”转换为“一千二百六十”)。
* 下面的宏经过测试,正确无误(稍后我会将此宏加入新编《第一条超强版》宏,最大可以处理 9999 条法律条文),有需要的朋友请试用:
- Sub Num2Chn()
- '数字转汉字/TEST-OK
- Dim strNum$, lngLen&, lngCnt&, SglNum$, strSum$
- strNum = 0
- lngLen = Len(strNum)
- strSum = ""
- For lngCnt = 1 To lngLen
- SglNum = Mid(strNum, lngCnt, 1)
- SglNum = Replace(SglNum, "1", "一")
- SglNum = Replace(SglNum, "2", "二")
- SglNum = Replace(SglNum, "3", "三")
- SglNum = Replace(SglNum, "4", "四")
- SglNum = Replace(SglNum, "5", "五")
- SglNum = Replace(SglNum, "6", "六")
- SglNum = Replace(SglNum, "7", "七")
- SglNum = Replace(SglNum, "8", "八")
- SglNum = Replace(SglNum, "9", "九")
- SglNum = Replace(SglNum, "0", "零")
- strSum = strSum & SglNum
- Next
- If lngLen = 2 Then
- strSum = Left(strSum, 1) & "十" & Right(strSum, 1)
- strSum = Replace(strSum, "一十", "十")
- strSum = Replace(strSum, "十零", "十")
- ElseIf lngLen = 3 Then
- strSum = Left(strSum, 1) & "百" & Mid(strSum, 2, 1) & "十" & Right(strSum, 1)
- strSum = Replace(strSum, "十零", "十")
- strSum = Replace(strSum, "零十", "零")
- If strSum Like "?百零" Then strSum = Replace(strSum, "百零", "百")
- ElseIf lngLen = 4 Then
- strSum = Left(strSum, 1) & "千" & Mid(strSum, 2, 1) & "百" & Mid(strSum, 3, 1) & "十" & Right(strSum, 1)
- strSum = Replace(strSum, "十零", "十")
- strSum = Replace(strSum, "零十", "零")
- strSum = Replace(strSum, "零百", "零")
- If Not strSum Like "*百零?" Then strSum = Replace(strSum, "百零", "百")
- strSum = Replace(strSum, "零零", "零")
- If strSum Like "?千零" Then strSum = Replace(strSum, "千零", "千")
- End If
- MsgBox strNum & "/" & strSum
- End Sub
复制代码 |