对于从事金融或者会计行业的朋友,最常遇到的问题就是人民币大写金额与阿拉伯数字的相互转化问题。毕竟,对于发票、合同等正规文件中,需要使用人民币大写金额,所以就需要将阿拉伯数字转化为大写金额;但是针对于大写金额,我们又没有办法直接进行求和、相加减的运算操作,所以我们在计算金额前,又需要将大写金额转化为阿拉伯数字。 那么如何实现快速切换人民币大写金额与阿拉伯数字就显得尤为重要! 人民币大写金额与阿拉伯数字的切换可以分为以下方面:
将数字转为大写金额,可以通过设置单元格的格式为'中文大写数字'(具体步骤:选中单元格->数字->其他数字格式->特殊->中文大写数字),通过设置单元格格式实现的中文金额大写可以直接运算 如果不希望修改格式,可以通过自定义函数使用'WorksheetFunction.Text(source.Text, '[DBNum2]')',这种方式会把数字转化为中文大写,如果需要显示元角分,需要进行特殊处理 将大写金额转化为数字,可以通过建立阿拉伯数字与大写金额的匹配,然后通过遍历替换,得到每一个大写金额对应的阿拉伯数字,实现切换。这里需要特殊处理人民币的分位符如'拾、佰、仟、万、亿、兆、元、角、分、零、整'
'将阿拉伯数字转化为中文大写数字,即金额大写 Function NumberToRMB(source As Range) '通过Excel内置Text函数实现数字转中文大写 result = WorksheetFunction.Text(source.Text, '[DBNum2]') '针对带小数的数字,特殊处理金额 If InStr(result, '.') Then yuan = Mid(result, 1, InStr(result, '.') - 1) jiao = Mid(result, InStr(result, '.') + 1, 1) fen = Mid(result, InStr(result, '.') + 2, 1) '处理元、角、分 If yuan <> '' Then NumberToRMB = yuan & '元' End If If jiao <> '' Then NumberToRMB = NumberToRMB & jiao & '角' End If If fen <> '' Then NumberToRMB = NumberToRMB & fen & '分' End If Else NumberToRMB = result & '元' End If End Function
'将金额大写(中文大写数字)转化为阿拉伯数字 Function RMBToNumber(source As Range) Dim dict As Object Dim unitDict As Object Set dict = CreateObject('Scripting.Dictionary') Set unitDict = CreateObject('Scripting.Dictionary') '预定义中文大写与阿拉伯数字的对应 chineseNum = '零壹贰叁肆伍陆柒捌玖' num = '0123456789' '将中文大写与数字加入到集合dict中,便于后续匹配 For i = 1 To Len(num) dict.Add Mid(chineseNum, i, 1), Mid(num, i, 1) Next i '预定义单位 chinese_uom = '拾、佰、仟、万、亿、兆、元、角、分、零、整' num_uom = '10、100、1000、10000、100000000、1000000000000、1、0.1、0.01、1、1' chinese_uom_split = Split(chinese_uom, '、') num_uom_split = Split(num_uom, '、') For i = 0 To UBound(chinese_uom_split) unitDict.Add chinese_uom_split(i), num_uom_split(i) Next temp = 0 '遍历中文大写,依次替换每一个字符 sourceText = source.Text For i = 1 To Len(sourceText) Debug.Print Mid(sourceText, i, 1) current = Mid(sourceText, i, 1) '判断是否是常规大写,直接替换 If dict.exists(current) Then Debug.Print dict(current) temp = temp + CInt(dict(current)) '如果是单位大写,特殊处理 ElseIf unitDict.exists(current) Then If current = '兆' Then result = result + temp result = result * unitDict(current) zhao = result result = 0 ElseIf current = '亿' Then result = result + temp result = result * unitDict(current) yi = result result = 0 ElseIf current = '万' Then result = result + temp result = result * unitDict(current) wan = result result = 0 Else temp = temp * unitDict(current) result = result + temp End If temp = 0 End If Debug.Print result Next result = zhao + yi + wan + result + temp '输出最终转化后的结果 RMBToNumber = result End Function
|