|
楼主 |
发表于 2015-10-29 16:09
|
显示全部楼层
VBA(Module2)代码:
- Sub DisplayRedundantCalc(ByVal control As Office.IRibbonControl)
- ' 选择正确的过程.
- Select Case CalcType
- Case "Loan"
- PerformLoanRangeCalc
- Case "Annuity"
- PerformAnnuityRangeCalc
- Case "EffectiveRate"
- PerformEffectiveRateRangeCalc
- End Select
- End Sub
- Sub PerformLoanRangeCalc()
- msg = InputBox("利率开始值/利率最终值/利率增加量/期数开始值/期数率最终值/初始金额:", "请输入批量信息:", "1/12/1/" & IIf(CalcType = "Loan", 10, 5) & "/" & IIf(CalcType = "Loan", 30, 20) & "/105000")
- Rate = Val(Split(msg, "/")(0))
- Term = Val(Split(msg, "/")(3))
- Amount = Val(Split(msg, "/")(5))
-
- ' 创建本地变量,包括计算数据.
- EndRate = Val(Split(msg, "/")(1))
- IncRate = Val(Split(msg, "/")(2))
- EndTerm = Val(Split(msg, "/")(4))
-
- ' 添加初始标题.
- Application.Selection.Cells(1, 1) = "利息"
-
- ' 执行计算.
- Dim i As Integer
- For i = Rate To EndRate
- ' 计算X和Y的位置值.
- X = i + 2 - Rate
- Y = 2
- Application.Selection.Cells(X, 1) = Format(i / 100, "#%")
- ' 使用一系列if语句确定年设置.
- If Term = 10 And EndTerm >= 10 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=PMT(" & i / 100 / 12 & "," & 10 * 12 & "," & Amount & ",0,0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "10年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 15 And EndTerm >= 15 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=PMT(" & i / 100 / 12 & "," & 15 * 12 & "," & Amount & ",0,0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "15年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 20 And EndTerm >= 20 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=PMT(" & i / 100 / 12 & "," & 20 * 12 & "," & Amount & ",0,0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "20年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 30 And EndTerm >= 30 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=PMT(" & i / 100 / 12 & "," & 30 * 12 & "," & Amount & ",0,0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "30年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
- Next
- End Sub
-
- Sub PerformAnnuityRangeCalc()
- msg = InputBox("利率开始值/利率最终值/利率增加量/期数开始值/期数率最终值/初始金额/支付金额:", "请输入批量信息:", "1/12/1/" & IIf(CalcType = "Loan", 10, 5) & "/" & IIf(CalcType = "Loan", 30, 20) & "/500/500")
- Rate = Val(Split(msg, "/")(0))
- Term = Val(Split(msg, "/")(3))
- Amount = Val(Split(msg, "/")(5))
- Payment = Val(Split(msg, "/")(6))
-
- ' 创建本地变量,包括计算数据.
- EndRate = Val(Split(msg, "/")(1))
- IncRate = Val(Split(msg, "/")(2))
- EndTerm = Val(Split(msg, "/")(4))
-
- ' 添加初始标题.
- Application.Selection.Cells(1, 1) = "利息"
-
- ' 执行计算.
- Dim i As Integer
- For i = Rate To EndRate
- ' 计算X和Y的位置值.
- X = i + 2 - Rate
- Y = 2
- Application.Selection.Cells(X, 1) = Format(i / 100, "#%")
-
- If Term = 5 And EndTerm >= 5 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=FV(" & i / 100 / 12 & "," & 5 * 12 & "," & Payment & "," & Amount & ",0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "5年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 7 And EndTerm >= 7 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=FV(" & i / 100 / 12 & "," & 7 * 12 & "," & Payment & "," & Amount & ",0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "7年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 10 And EndTerm >= 10 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=FV(" & i / 100 / 12 & "," & 10 * 12 & "," & Payment & "," & Amount & ",0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "10年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 15 And EndTerm >= 15 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=FV(" & i / 100 / 12 & "," & 15 * 12 & "," & Payment & "," & Amount & ",0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "15年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
-
- If Term <= 20 And EndTerm >= 20 Then
- ' 执行计算.
- Application.Selection.Cells(X, Y) = "=FV(" & i / 100 / 12 & "," & 20 * 12 & "," & Payment & "," & Amount & ",0)"
- ' 打印标题.
- Application.Selection.Cells(1, Y) = "20年"
- ' 如果已经使用则增加Y.
- Y = Y + 1
- End If
- Next
- End Sub
-
- Sub PerformEffectiveRateRangeCalc()
- msg = InputBox("利率开始值/利率最终值/利率增加量:", "请输入批量信息:", "1/12/1")
- Rate = Val(Split(msg, "/")(0))
-
- ' 创建本地变量,包括计算数据.
- EndRate = Val(Split(msg, "/")(1))
- IncRate = Val(Split(msg, "/")(2))
-
- ' 添加初始标题.
- Application.Selection.Cells(1, 1) = "利息"
- Application.Selection.Cells(1, 2) = "有效利率"
-
- ' 执行计算.
- Dim i As Integer
- For i = Rate To EndRate
- ' 计算X和Y的位置值.
- X = i + 2 - Rate
- Application.Selection.Cells(X, 1) = Format(i / 100, "#%")
- ' 执行计算.
- Application.Selection.Cells(X, 2) = "=EFFECT(" & i / 100 / 12 & ",12)"
- 'Globals.ThisAddIn.CalculateEFFECT(i, X, 2)
- Next
- End Sub
复制代码 |
|