|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
近几年一直在海外做财务,日常工作中经常涉及将小写金额转换为英文大写,学习VBA已有两个多月,这两天工作之余抽空编写了一个货币金额自动转换为英文大写的公式,分享给大家,望赐教。如果大家有更好的解决方案,希望能一起探讨交流。
该公式对10亿以内的数字,可以进行有效转换,已经过实测检验。如果需转换10亿以上的数字,可添加代码。不过日常汇款/账务操作,一般10亿内够用了。下面是公式代码:
Function ConVtoEng(n As Single, Optional Curr As String)
Dim ArrN(0 To 9, 0 To 9), ArrH(0 To 9), nz As Long, n1 As Byte, n2 As Byte, n3 As Byte, n4 As Byte, n5 As Byte, n6 As Byte, n7 As Byte, n8 As Byte, n9 As Byte, s1 As Byte, s2 As Byte, xs As Byte
If Not IsNumeric(n) Then ConVtoEng = "#N/A"
If n > 1000000000 Then ConVtoEng = "#N/A"
'将英文0-99赋值给数值Arrn
ArrN(0, 0) = "": ArrN(0, 1) = "One": ArrN(0, 2) = "Two": ArrN(0, 3) = "Three": ArrN(0, 4) = "Four": ArrN(0, 5) = "Five": ArrN(0, 6) = "Six": ArrN(0, 7) = "Seven": ArrN(0, 8) = "Eight": ArrN(0, 9) = "Nine"
ArrN(1, 0) = "Ten": ArrN(1, 1) = "Eleven": ArrN(1, 2) = "Twelve": ArrN(1, 3) = "Thirteen": ArrN(1, 4) = "Fourteen": ArrN(1, 5) = "Fifteen": ArrN(1, 6) = "Sixteen": ArrN(1, 7) = "Seventeen": ArrN(1, 8) = "Eighteen": ArrN(1, 9) = "Nineteen"
ArrN(2, 0) = "Twenty": ArrN(3, 0) = "Thirty": ArrN(4, 0) = "Forty": ArrN(5, 0) = "Fifty": ArrN(6, 0) = "Sixty": ArrN(7, 0) = "Seventy": ArrN(8, 0) = "Eighty": ArrN(9, 0) = "Ninety"
Dim i As Byte, y As Byte
'通过循环将21-99赋值给数组ArrN
For i = 2 To 9
For y = 1 To 9
ArrN(i, y) = ArrN(i, 0) & "-" & ArrN(0, y)
Next y
Next i
'将100-900整数百赋值给数组ArrH
ArrH(0) = ""
For i = 1 To 9
ArrH(i) = ArrN(0, i) & " Hundred"
Next i
xs = (Round(n, 2) - Int(n)) * 100 '提取两位小数位数字
'依次提取整数位各位上的数值,此处运用Mod和Int函数
n = Int(n)
n1 = Int(n Mod 10)
n2 = Int((n Mod 100) / 10)
n3 = Int((n Mod 1000) / 100)
n4 = Int((n Mod 10000) / 1000)
n5 = Int((n Mod 100000) / 10000)
n6 = Int((n Mod 1000000) / 100000)
n7 = Int((n Mod 10000000) / 1000000)
n8 = Int((n Mod 100000000) / 10000000)
n9 = Int((n Mod 1000000000) / 100000000)
'先对小数部分进行转换
Dim X_Str$
If xs = 0 Then
X_Str = " Only"
Else: X_Str = " and " & "Cents " & ArrN(Int(xs / 10), (xs Mod 10)) & " Only" '100以内数字直接引用Arrn数组中的值
End If
'定义各位上的字符变量
Dim StrH1$, StrT1$, u$, StrH2$, StrT2$, N_Str$, N_Str1$, StrH3$, StrT3$, N_Str2$ 'StrH对应Hundred,取数自数组Arrh;StrT对应十和个位,取数自ArrN数组
Select Case n
Case Is = 0
N_Str = "Zero" & X_Str
Case Is < 1000
StrH1 = ArrH(n3): StrT1 = ArrN(n2, n1) '引用数组中的字符
If StrH1 <> "" And StrT1 <> "" Then
N_Str = StrH1 & " and " & StrT1 & X_Str
Else
N_Str = StrH1 & " " & StrT1 & X_Str
End If
Case Is < 1000000
StrH1 = ArrH(n3): StrT1 = ArrN(n2, n1): StrH2 = ArrH(n6): StrT2 = ArrN(n5, n4) '引用数组中的字符
If StrT1 = "" Then
N_Str1 = StrH1 & StrT1 & X_Str
Else
N_Str1 = StrH1 & " and " & StrT1 & X_Str
End If
If StrH2 <> "" And StrT2 <> "" Then
N_Str = StrH2 & " and " & StrT2 & " Thousand " & N_Str1
Else
N_Str = StrH2 & " " & StrT2 & " Thousand " & N_Str1
End If
Case Is < 1000000000
StrH1 = ArrH(n3): StrT1 = ArrN(n2, n1): StrH2 = ArrH(n6): StrT2 = ArrN(n5, n4)
StrH3 = ArrH(n9): StrT3 = ArrN(n8, n7)
If StrT1 = "" Then
N_Str1 = StrH1 & StrT1 & X_Str
Else
N_Str1 = StrH1 & " and " & StrT1 & X_Str
End If
If StrT2 <> "" Then
N_Str2 = StrH2 & " and " & StrT2 & " Thousand " & N_Str1
ElseIf StrH2 <> "" Then
N_Str2 = StrH2 & " Thousand " & N_Str1
Else
N_Str2 = N_Str1
End If
If StrH3 <> "" And StrT3 <> "" Then
N_Str = StrH3 & " and " & StrT3 & " Million " & N_Str2
Else
N_Str = StrH3 & " " & StrT3 & " Million " & N_Str2
End If
End Select
ConVtoEng = Curr & " " & N_Str
End Function
|
|