网站首页 > 技术文章 正文
在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
- 上一篇: 面试常见的四种算法思想,全在这里了
- 下一篇: 隐藏在一段文字中的数值,我让你无所遁形
猜你喜欢
- 2024-09-18 七牛对象存储(七牛对象存储价格)
- 2024-09-18 PHP 10个最具影响力的新功能(php 10个最具影响力的新功能有哪些)
- 2024-09-18 隐藏在一段文字中的数值,我让你无所遁形
- 2024-09-18 Excel VBA 新手学习笔记 字典基础导论
- 2024-09-18 面试常见的四种算法思想,全在这里了
- 2024-09-18 分享自定义函数,根据单元格格式统计数据,比宏表函数好用得多
- 2024-09-18 Spring Cloud Function 快速入门(spring cloud讲解)
- 2024-09-18 ExcelStat特殊函数计算(2):不完全伽马函数
- 2024-09-18 你加班 1 小时做表格,我用VBA只需30秒钟,直接粘贴拿去用吧
- 2024-09-18 设计模式之装饰器模式(装饰器模式实现)
- 1512℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 556℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 505℃MySQL service启动脚本浅析(r12笔记第59天)
- 483℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 482℃启用MySQL查询缓存(mysql8.0查询缓存)
- 462℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 442℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 439℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)