观看视频更直观
在Excel中将数字转换为中文大写或小写,有两种方法,一种是设置数据格式;另一种是使用内置的隐蔽函数NumberString函数。
一、设置数据格式
在Excel中输入数字后,选中单元格,在"开始"选项卡中单击"数字"组右下角对话框启动按钮,弹出"设置单元格格式"对话框,在"数字"选项"分类"列表中选中"特殊",在右边"类型"框中根据需要选择"中文大写数字"或"中文小写数字"。这种内置的格式有两个缺陷:⑴设置数据格式仅仅只是改变了数据的显示形式,数据的本质没有变化,当使用邮件合并功能,格式化后的数据在Word中依然是原始数据形态;当我们对格式化后的数据进行"选择性粘贴",显示的也是原始数据。⑵设置数据格式进行中文大小写转换,会丢失一些必要的"零",这就不符合数学的转换规范。
二、使用NumberString函数
NumberString函数是Excel中隐藏的一个函数,可以将数字转换为中文大小写,函数只能通过手工输入的方法进行录入,而不能在"插入函数"对话框中插入。
函数语法:NumberString(要转换的数字, 转换类型),第一个参数是要转换的数字(必需),第二个参数是转换类型(必需),设置为1:转换为中文大写;设置为2:转换为中文小写;设置为3:仅将数字作为字符(而不是作为数字)进行转换,返回中文小写。如下图:
NumberString函数有两个限制:⑴只能转换整数,如果是小数,将对小数点之后的数字四舍五入;只能转换正数,如果第一个参数是负数,将会报错,错误类型为"#NUM!",也就是可能在需要数字参数的函数中提供了错误的数据类型。⑵财会工作中也需要经常将金额转换为中文大写,单位除整数的"元"这个单位,有时还要带角分小数。
三、自定义函数NumToDaxie函数
在Excel中要转换中文大小写,不管是设置数据格式,还是使用内置的隐藏函数NumberString函数,都有缺陷和限制,于是编写了一个自定义函数NumToDaxie来解决实际需要。
函数语法:NumToDaxie (要转换的数字, [转换类型],第一个参数是要转换的数字(必需),第二个参数是转换类型(可选),设置为0或省略:转换为元角分财会人员专用格式;设置为1:转换为中文大写;转换中文小写内置函数已经可以满足实际需要,故没有设置。如下图:
四、NumToDaxie函数
打开Excel后新建一个工作簿,右击sheet1工作表标签,在弹出的快捷菜单中选择"查看代码"命令,打开VBA窗口,单击"插入"菜单→"模块"命令,将下面的代码复制到插入的"模块1"即可,注意有两个函数。
Public Function NumToDaxie(DblNumber As Double, Optional iType As Byte = 0) As String
Dim StrNum As String, StrNumL As String, StrNumM As String, strNumR As String
Dim i As Integer, j As Integer, StrB As String, strXS As String, strZF As String
strXS = IIf(iType = 1, "", "元整")
DblNumber = Round(DblNumber, 2) '保留两位小数
StrNum = Trim(Str(DblNumber))
If Left(StrNum, 1) = "-" Or Left(StrNum, 1) = "(" Then
StrNum = Mid(StrNum, 2, Len(StrNum) - 1)
strZF = "负"
End If
i = InStr(DblNumber, ".") '搜索小数点,如果有则i返回小数点位置
If i <> 0 Then
xName = Array("", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
strNumR = Mid(StrNum, i + 1, Len(StrNum) - i) '获取小数点右侧数字
StrNum = Left(StrNum, i - 1) '获取小数点左侧数字
'以小数点分隔右、左两部分,先右后左,顺序不能颠倒
Select Case Len(strNumR)
Case 1
If iType = 1 Then
strXS = "点" & xName(Val(strNumR))
Else
strXS = "元" & xName(Val(strNumR)) & "角"
End If
Case 2
If iType = 1 Then
strXS = "点" & xName(Val (Left(strNumR, 1))) & xName( Val(Right(strNumR, 1)))
Else
strXS = "元" & xName( Val(Left(strNumR, 1))) & "角" & _
xName(Val(Right (strNumR, 1))) & "分"
End If
End Select
End If
StrNum = Format(StrNum, "000000000000")
StrNumL = Left(StrNum, 4)
StrNumM = Mid(StrNum, 5, 4)
strNumR = Right(StrNum, 4)
If ZWDX(StrNumL) <> "" Then StrB = ZWDX(StrNumL) + "亿" Else StrB = "零"
If ZWDX(StrNumM) <> "" Then
StrB = StrB + ZWDX(StrNumM) + "万"
Else
StrB = StrB + ZWDX(StrNumM) + "零"
End If
StrB = StrB + ZWDX(strNumR)
'以下均是对数字进行数学规范调整,不可缺少
If InStr(StrB, "壹拾零亿") <> 0 Then StrB = Replace(StrB, "壹拾零亿", "拾亿零")
If InStr(StrB, "壹拾零万") <> 0 Then StrB = Replace(StrB, "壹拾零万", "拾万零")
If InStr(StrB, "零亿") <> 0 Then StrB = Replace(StrB, "零亿", "亿零")
If InStr(StrB, "零万") <> 0 Then StrB = Replace(StrB, "零万", "万零")
If InStr(StrB, "零零") <> 0 Then
Do Until InStr(StrB, "零零") = 0
StrB = Replace(StrB, "零零", "零")
Loop
End If
Do While Left(StrB, 1) = "零"
StrB = Right(StrB, Len(StrB) - 1)
Loop
Do While Right(StrB, 1) = "零"
StrB = Left(StrB, Len(StrB) - 1)
Loop
NumToDaxie = strZF & StrB & strXS
End Function
Public Function ZWDX(Strnumber As String) As String
'将一个四位数转换为中文大写
Dim StrName(3) As String, IntName(3) As Integer, StrA As String
Name1 = Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
Name2 = Array("", "拾", "佰", "仟")
Strnumber = Trim(Strnumber)
If Strnumber = "0000" Then
ZWDX = ""
Exit Function
End If
For i = 3 To 0 Step -1
StrName(i) = Mid(Strnumber, 4 - i, 1)
IntName(i) = Val(StrName(i))
If IntName(i) = 0 Then
StrName(i) = Name1(IntName(i))
Else
StrName(i) = Name1(IntName(i)) + Name2(i)
End If
StrA = StrA + StrName(i)
Next i
If InStr(StrA, "零零") <> 0 Then
Do Until InStr(StrA, "零零") = 0
StrA = Replace(StrA, "零零", "零")
Loop '去掉转换过程中产生的多余的零,使之符合数学规范
End If
ZWDX = StrA
End Function