|
楼主 |
发表于 2011-8-25 13:00
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
加法改版
1. 不是每次1个数位的计算,而是以长度12为单位计算,这样加快了速度。
2. 增加了k参数,可以指定结果:
k=0 按实际正负号计算。
k=1 指定输出Na+Nb的正结果
k=2 指定输出-(Na+Nb)结果
k=3 指定输出Na-Nb的结果
k=4 指定输出Nb-Na的结果- Function PM(Na, Nb, Optional k = 0) '桁数多い文字表示数字の加減計算
- ka = IIf(Left(Na, 1) = "-", "-", "+")
- If Not IsNumeric(Left(Na, 1)) Then Na = Mid(Na, 2, Len(Na))
- Pa = IIf(InStr(Na, ".") = 0, 0, Len(Na) - InStr(Na, "."))
- If Left(Na, 2) = "0." Then Na = Replace(Na, "0.", "") Else Na = Replace(Na, ".", "")
-
- kb = IIf(Left(Nb, 1) = "-", "-", "+")
- If Not IsNumeric(Left(Nb, 1)) Then Nb = Mid(Nb, 2, Len(Nb))
- Pb = IIf(InStr(Nb, ".") = 0, 0, Len(Nb) - InStr(Nb, "."))
- If Left(Nb, 2) = "0." Then Nb = Replace(Nb, "0.", "") Else Nb = Replace(Nb, ".", "")
-
- If Pa > Pb Then p = Pa Else p = Pb
- Na = Na & String(p - Pa, "0")
- Nb = Nb & String(p - Pb, "0")
-
- l = IIf(Len(Na) > Len(Nb), Len(Na) + 1, Len(Nb) + 1) \ 12
- ReDim a(l + 2, 4)
- For i = 1 To l + 1
- If i * 12 <= Len(Na) Then a(i, 1) = Mid(Na, Len(Na) - i * 12 + 1, 12) Else If i = Len(Na) \ 12 + 1 Then a(i, 1) = Left(Na, Len(Na) - (i - 1) * 12)
- If i * 12 <= Len(Nb) Then a(i, 2) = Mid(Nb, Len(Nb) - i * 12 + 1, 12) Else If i = Len(Nb) \ 12 + 1 Then a(i, 2) = Left(Nb, Len(Nb) - (i - 1) * 12)
-
- If Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) < 10 ^ 12 Then a(i, 0) = Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) Else a(i + 1, 0) = 1: a(i, 0) = Val(a(i, 0)) + Val(a(i, 1)) + Val(a(i, 2)) - 10 ^ 12
- a(0, 0) = Right(String(12, "0") & a(i, 0), 12) & a(0, 0)
-
- If Val(a(i, 3)) + Val(a(i, 1)) < Val(a(i, 2)) Then a(i + 1, 3) = -1: a(i, 3) = 10 ^ 12 + Val(a(i, 3)) + Val(a(i, 1)) - Val(a(i, 2)) Else a(i, 3) = Val(a(i, 3)) + Val(a(i, 1)) - Val(a(i, 2))
- a(0, 3) = Right(String(12, "0") & a(i, 3), 12) & a(0, 3)
-
- If Val(a(i, 4)) + Val(a(i, 2)) < Val(a(i, 1)) Then a(i + 1, 4) = -1: a(i, 4) = 10 ^ 12 + Val(a(i, 4)) + Val(a(i, 2)) - Val(a(i, 1)) Else a(i, 4) = Val(a(i, 4)) + Val(a(i, 2)) - Val(a(i, 1))
- a(0, 4) = Right(String(12, "0") & a(i, 4), 12) & a(0, 4)
- Next i
-
- a(0, 0) = a(l + 2, 0) & a(0, 0)
- a(0, 3) = a(l + 2, 3) & a(0, 3)
- a(0, 4) = a(l + 2, 4) & a(0, 4)
-
- If p > 0 Then
- a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - p) & "." & Right(a(0, 0), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 0), 1) = "0" Then a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - 1) Else Exit For
- Next
- If Right(a(0, 0), 1) = "." Then a(0, 0) = Left(a(0, 0), Len(a(0, 0)) - 1)
-
- a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - p) & "." & Right(a(0, 3), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 3), 1) = "0" Then a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - 1) Else Exit For
- Next
- If Right(a(0, 3), 1) = "." Then a(0, 3) = Left(a(0, 3), Len(a(0, 3)) - 1)
-
- a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - p) & "." & Right(a(0, 4), p)
- For i = 1 To (l + 1) * 12
- If Right(a(0, 4), 1) = "0" Then a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - 1) Else Exit For
- Next
- If Right(a(0, 4), 1) = "." Then a(0, 4) = Left(a(0, 4), Len(a(0, 4)) - 1)
-
- End If
-
- For i = 1 To (l + 1) * 12
- If Left(a(0, 0), 1) = "0" And Left(a(0, 0), 2) <> "0." Then a(0, 0) = Right(a(0, 0), Len(a(0, 0)) - 1) Else Exit For
- Next
- For i = 1 To (l + 1) * 12
- If Left(a(0, 3), 1) = "0" And Left(a(0, 3), 2) <> "0." Then a(0, 3) = Right(a(0, 3), Len(a(0, 3)) - 1) Else Exit For
- Next
- For i = 1 To (l + 1) * 12
- If Left(a(0, 4), 1) = "0" And Left(a(0, 4), 2) <> "0." Then a(0, 4) = Right(a(0, 4), Len(a(0, 4)) - 1) Else Exit For
- Next
-
- If Left(a(0, 3), 1) = "-" Then a(0, 3) = "-" & a(0, 4)
- If Left(a(0, 4), 1) = "-" Then a(0, 4) = "-" & a(0, 3)
-
- If k = 0 Then
- If ka = "+" And kb = "+" Then
- PM = a(0, 0)
- ElseIf ka = "+" And kb = "-" Then
- PM = a(0, 3)
- ElseIf ka = "-" And kb = "+" Then
- PM = a(0, 4)
- ElseIf ka = "-" And kb = "-" Then
- PM = "-" & a(0, 0)
- End If
- ElseIf k = 1 Then
- PM = a(0, 0)
- ElseIf k = 2 Then
- PM = "-" & a(0, 0)
- ElseIf k = 3 Then
- PM = a(0, 3)
- ElseIf k = 4 Then
- PM = a(0, 4)
- End If
-
- End Function
复制代码 |
|