ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: weiyingde

[求助] 求一个中文小写自定义函数:函数中回避使用worksheetfunction

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-3-2 16:04 | 显示全部楼层
上面代码小数只能认出2位,我重新从网上找了个代码完善了下:
  1. Function CChinese(StrEng As String) As String
  2.     If Not IsNumeric(StrEng) Then
  3.         If Trim(StrEng) <> "" Then MsgBox "无效的数字"
  4.         CChinese = "": Exit Function
  5.     End If
  6.    
  7.     Dim intLen As Integer, intCounter As Integer
  8.     Dim strCh As String, strTempCh As String
  9.     Dim strSeqCh1 As String, strSeqCh2 As String
  10.     Dim strEng2Ch As String, strDec As String
  11.    
  12.     strEng2Ch = "〇一二三四五六七八九"
  13.     strSeqCh1 = " 十百千 十百千 十百千 十百千"
  14.     strSeqCh2 = " 万亿兆"
  15.    
  16.     If InStr(StrEng, ".") Then strDec = Split(StrEng, ".")(1)
  17.     StrEng = CStr(Int(StrEng))

  18.     '整数部分
  19.     intLen = Len(StrEng)
  20.     For intCounter = 1 To intLen
  21.         strTempCh = Mid(strEng2Ch, Val(Mid(StrEng, intCounter, 1)) + 1, 1)
  22.         If strTempCh = "〇" And intLen <> 1 Then
  23.             If Mid(StrEng, intCounter + 1, 1) = "0" Or (intLen - intCounter + 1) Mod 4 = 1 Then
  24.                 strTempCh = ""
  25.             End If
  26.         Else
  27.             strTempCh = strTempCh & Trim(Mid(strSeqCh1, intLen - intCounter + 1, 1))
  28.         End If
  29.    
  30.         If (intLen - intCounter + 1) Mod 4 = 1 Then
  31.             strTempCh = strTempCh & Mid(strSeqCh2, (intLen - intCounter + 1) \ 4 + 1, 1)
  32.             If intCounter > 3 Then
  33.                 If Mid(StrEng, intCounter - 3, 4) = "0000" Then strTempCh = Left(strTempCh, Len(strTempCh) - 1)
  34.             End If
  35.         End If
  36.         strCh = strCh & Trim(strTempCh)
  37.     Next
  38.    
  39.     '小数部分
  40.     If Len(strDec) Then
  41.         For i = 0 To 9
  42.             strDec = Replace(strDec, i, Mid(strEng2Ch, i + 1, 1))
  43.         Next
  44.         strCh = strCh & "点" & strDec
  45.     End If
  46.    
  47.     CChinese = strCh
  48. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2020-3-2 17:09 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 yjh_27 于 2020-3-3 13:01 编辑
weiyingde 发表于 2020-3-2 14:38
那就凑合着用吧,学艺不精,只好将就着。
  1. Function a2cc(s, Optional m = 0)
  2. If s < 0 Then
  3. t = "负"
  4. s = -s
  5. Else
  6. t = ""
  7. End If

  8. yfl = Split(s, ".")
  9. If m = 0 Then
  10.     tm = Split("零 壹 贰 叁 肆 伍 陆 柒 捌 玖")
  11. Else
  12.     tm = Split("〇 一 二 三 四 五 六 七 八 九")
  13. End If
  14. If m = 0 Then
  15.     tk = Split("亿 万 仟 佰 拾")
  16. Else
  17.     tk = Split("亿 万 千 百 十")
  18. End If

  19. L = Len(yfl(0)) - 1
  20. t0 = 0.1
  21. yi = L \ 8
  22. ReDim y(yi)
  23. For i = 0 To yi
  24.     y(i) = Right(yfl(0), 8)
  25.     If Len(yfl(0)) > 8 Then yfl(0) = Left(yfl(0), Len(yfl(0)) - 8)
  26. Next

  27. For i = yi To 0 Step -1
  28.     LW = Len(y(i)) - 1
  29.     wi = LW \ 4
  30.     ReDim w(wi)
  31.     For ii = 0 To wi
  32.         w(ii) = Right(y(i), 4)
  33.         If Len(y(i)) > 4 Then y(i) = Left(y(i), Len(y(i)) - 4)
  34.     Next
  35.     For ii = wi To 0 Step -1
  36.         L = Len(w(ii))
  37.         For iii = 1 To L
  38.             t1 = Mid(w(ii), iii, 1)
  39.             If t0 > 0 Or t1 > 0 Then
  40.                  t = t & tm(t1)
  41.                  L1 = L + 1 - iii
  42.                  If t1 > 0 Then If L1 >= 2 And L1 <= 4 Then t = t & tk(6 - L1)
  43.                  t0 = t1
  44.             End If
  45.         Next
  46.         
  47.         If ii > 0 Then
  48.             If t0 = 0 Then
  49.                 t = Mid(t, 1, Len(t) - 1) & tk(1) & tm(0)
  50.             Else
  51.                 t = t & tk(1)
  52.             End If
  53.         End If
  54.     Next
  55.     If i > 0 Then
  56.         If t0 = 0 Then
  57.             t = Mid(t, 1, Len(t) - 1) & tk(0) & tm(0)
  58.         Else
  59.             t = t & tk(0)
  60.         End If
  61.     End If
  62. Next

  63. If UBound(yfl) > 0 Then
  64.     If t = "" Then
  65.         t = tm(0) & "点"
  66.     ElseIf Right(t, 1) = tm(0) Then
  67.         Mid(t, Len(t), 1) = "点"
  68.     Else
  69.         t = t & "点"
  70.     End If
  71.     L = Len(yfl(1))
  72.     For i = 1 To L
  73.         t = t & tm(Mid(yfl(1), i, 1))
  74.     Next
  75. End If
  76. t = Replace(t, tk(0) & tk(1), tk(0))
  77. If Right(t, 1) = tm(0) Then t = Mid(t, 1, Len(t) - 1)
  78. a2cc = t
  79. End Function
复制代码
与楼下不同处101000     壹拾万零壹仟       一十万一千


复制代码


评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2020-3-2 18:35 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-3-2 19:13 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 mzbao 于 2020-3-2 19:43 编辑

自己写了个。和上面的思路完全不一样。
  1. Function NumToCN(ByVal ArabicNum) As String
  2.     If Not IsNumeric(ArabicNum) Then
  3.         If Trim(ArabicNum) <> "" Then NumToCN = "N/A" Else NumToCN = ""
  4.         Exit Function
  5.     End If
  6.    
  7.     Dim strInt&, strDec$, i%, strNumCN$, strTempCN$, strMinus$
  8.    
  9.     strNumCN = "〇一二三四五六七八九"
  10.    
  11.     If ArabicNum < 0 Then strMinus = "负"
  12.     If InStr(ArabicNum, ".") Then strDec = Split(ArabicNum, ".")(1)
  13.     ArabicNum = Int(Abs(ArabicNum))
  14.     strTempCN = Format(ArabicNum, "0千0百0十0兆0千0百0十0亿0千0百0十0万0千0百0十0")
  15.     For i = 1 To 3: strTempCN = Replace(strTempCN, 0 & Mid("千百十", i, 1), 0): Next
  16.     For i = 1 To 3: strTempCN = Replace(strTempCN, "0000" & Mid("兆亿万", i, 1), ""): Next
  17.     For i = 1 To 3: strTempCN = Replace(strTempCN, "00", 0): Next
  18.     For i = 1 To 3: strTempCN = Replace(strTempCN, "0" & Mid("兆亿万", i, 1), Mid("兆亿万", i, 1)): Next
  19.     If ArabicNum = 0 Then strTempCN = "〇"
  20.     If Left(strTempCN, 1) = 0 Then strTempCN = Mid(strTempCN, 2)
  21.     If Right(strTempCN, 1) = 0 Then strTempCN = Left(strTempCN, Len(strTempCN) - 1)

  22.     If Len(strDec) Then strTempCN = strTempCN & "点" & strDec
  23.     For i = 0 To 9: strTempCN = Replace(strTempCN, i, Mid(strNumCN, i + 1, 1)): Next
  24.    
  25.     NumToCN = strMinus & strTempCN
  26. End Function
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-2 20:39 | 显示全部楼层

谢谢大侠,知道你的自定义函数做得好,果然出手不凡,谢谢啦。

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-2 20:40 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
mzbao 发表于 2020-3-2 19:13
自己写了个。和上面的思路完全不一样。

又收获一个,喜出望外。太感谢了。

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-2 20:47 | 显示全部楼层
本帖最后由 weiyingde 于 2020-3-2 20:49 编辑

两位的自定义函数,收进了我的帖子里。
http://club.excelhome.net/thread-1397953-7-1.html 64、65楼。
未经同意,不知可否。

TA的精华主题

TA的得分主题

发表于 2020-3-2 21:04 | 显示全部楼层
weiyingde 发表于 2020-3-2 14:29
你这是字母大小写吧?要实现1-一;2-二,3-三、4-四……这样的转换。

见4楼改正函数代码!!

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-2 21:47 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
lss001 发表于 2020-3-2 13:40
'*********************
Function numTotext(x)
Set ex = CreateObject("excel.application")

问一句,NUMBERSTRING是工作表函数吗。

TA的精华主题

TA的得分主题

发表于 2020-3-3 10:28 来自手机 | 显示全部楼层
weiyingde 发表于 2020-3-2 21:47
问一句,NUMBERSTRING是工作表函数吗。

NUMBERSTRING工作表非公开函数!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 10:26 , Processed in 0.040422 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表