|
我用VBA+XML做时失败,代码如下:
XML代码:
- <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
- <ribbon>
- <tabs>
- <tab id="myTab" label="专用等式" keytip="MT">
- <group id="Default" label="开始">
- <labelControl id="StartLabel1"
- label="选择一个"/>
- <labelControl id="StartLabel2"
- label="开始点"/>
- <labelControl id="StartLabel3"
- label="用于计算."/>
- </group>
- <group id="Equations" label="等式">
- <toggleButton id="Loan"
- label="贷款"
- onAction="SetupLoan"
- getPressed="SelectedEquation"/>
- <toggleButton id="Annuity"
- label="年金"
- onAction="SetupAnnuity"
- getPressed="SelectedEquation"/>
- <toggleButton id="EffectiveRate"
- label="有效利率"
- onAction="SetupEF"
- getPressed="SelectedEquation"/>
- </group>
- <group id="DataEntry" getLabel="GetDataEntryLabel">
- <editBox id="Rate"
- label="利率"
- onChange="GetRateText"/>
- <dropDown id="Term"
- label="期数"
- getVisible="TermVisible"
- getItemCount="TermCount"
- getItemID="TermItemID"
- getItemLabel="TermItemLabel"
- onAction="GetSelectedTerm"/>
- <editBox id="Payment"
- label="期初付款"
- getVisible="PaymentVisible"
- onChange="GetPaymentText"/>
- <editBox id="Amount"
- getLabel="AmountLabel"
- getVisible="AmountVisible"
- onChange="GetAmountText"/>
- </group>
- <group id="Finishcalculate" label="完成">
- <toggleButton id="Startcac"
- label="开始计算"
- onAction="Calculate"/>
- </group>
- </tab>
- </tabs>
- </ribbon>
- </customUI>
复制代码
VBA代码:
- Public CalcType
- Public Rate
- Public Term
- Public Payment
- Public Amount
-
- Sub SetupLoan(control As IRibbonControl, ByVal pressed As Boolean)
- ' 设置计算类型
- CalcType = "Loan"
- ' 设置按下状态
- pressed = True
- ' 使整个功能区无效
- ribbon.Invalidate
- End Sub
- Sub SetupAnnuity(control As IRibbonControl, ByVal pressed As Boolean)
- ' 设置计算类型
- CalcType = "Annuity"
- ' 设置按下状态
- pressed = True
- ' 使整个功能区无效
- ribbon.Invalidate
- End Sub
- Sub SetupEF(control As IRibbonControl, ByVal pressed As Boolean)
- ' 设置计算类型
- CalcType = "Effective Rate"
- ' 设置按下状态
- pressed = True
- ' 使整个功能区无效
- ribbon.Invalidate
- End Sub
- Function SelectedEquation(control As IRibbonControl) As Boolean
- ' 基于当前的等式确定按下状态
- Select Case CalcType
- Case "Loan"
- If control.ID = "Loan" Then
- SelectedEquation = True
- Else
- SelectedEquation = False
- End If
- Case "Annuity"
- If control.ID = "Annuity" Then
- SelectedEquation = True
- Else
- SelectedEquation = False
- End If
- Case "Effective Rate"
- If control.ID = "EffectiveRate" Then
- SelectedEquation = True
- Else
- SelectedEquation = False
- End If
- Case Else
- SelectedEquation = False
- End Select
- End Function
- Function TermVisible(control As IRibbonControl) As Boolean
- ' 应用程序不会使用该字段进行有效利率计算
- If CalcType = "Effective Rate" Then
- TermVisible = False
- Else
- TermVisible = True
- End If
- End Function
- Function PaymentVisible(control As IRibbonControl) As Boolean
- ' 当处理年金计算时应用程序仅使用该字段
- If CalcType = "Annuity" Then
- PaymentVisible = True
- Else
- PaymentVisible = False
- End If
- End Function
- Function AmountVisible(control As IRibbonControl) As Boolean
- ' 应用程序不会使用该字段进行有效利率计算
- If CalcType = "Effective Rate" Then
- AmountVisible = False
- Else
- AmountVisible = True
- End If
- End Function
- Function GetDataEntryLabel(control As IRibbonControl) As String
- ' 通过计算类型的选择决定组标签
- Select Case CalcType
- Case "Loan"
- GetDataEntryLabel = "输入贷款信息"
- Case "Annuity"
- GetDataEntryLabel = "输入年金信息"
- Case "Effective Rate"
- GetDataEntryLabel = "输入有效利率信息"
- Case Else
- GetDataEntryLabel = "没有实现!"
- End Select
- End Function
- Function AmountLabel(control As IRibbonControl) As String
- ' 通过计算类型的选择决定数量标签
- ' 由于有效利率计算不使用该控件,因此应用程序不给它提供标签
- Select Case CalcType
- Case "Loan"
- AmountLabel = "贷款金额"
- Case "Annuity"
- AmountLabel = "每月年金付款"
- Case Else
- AmountLabel = "没有实现!"
- End Select
- End Function
- Sub GetRateText(control As IRibbonControl, ByVal text As String)
- ' 保存文本的输入值
- Rate = Val(text)
- End Sub
- Sub GetSelectedTerm(control As IRibbonControl, ByVal selectedId As String, ByVal selectedIndex As Int32)
- ' 存储默认值
- Term = 0
- ' 保存贷款项
- If CalcType = "Loan" Then
- Select Case selectedIndex
- Case 0
- Term = 10
- Case 1
- Term = 15
- Case 2
- Term = 20
- Case 3
- Term = 30
- End Select
- End If
- ' 保存年金项
- If CalcType = "Annuity" Then
- Select Case selectedIndex
- Case 0
- Term = 5
- Case 1
- Term = 7
- Case 2
- Term = 10
- Case 3
- Term = 15
- Case 4
- Term = 20
- End Select
- End If
- End Sub
- Sub GetPaymentText(control As IRibbonControl, ByVal text As String)
- ' 保存文本的输入值
- Payment = Int32.Parse(text)
- End Sub
- Sub GetAmountText(control As IRibbonControl, ByVal text As String)
- ' 保存文本的输入值
- Amount = Int32.Parse(text)
- End Sub
- Sub Calculate(control As IRibbonControl)
- ' 选择计算并调用
- Select Case CalcType
- Case "Loan"
- CalculatePMT(Rate,Term,Amount)
- Case "Annuity"
- CalculateFV(Rate, Term, Payment, Amount)
- Case "Effective Rate"
- CalculateEFFECT (Rate)
- End Select
- End Sub
- ' 计算贷款数量
- Sub CalculatePMT(ByVal Rate As Double, ByVal NPer As Integer, ByVal PV As Integer)
- ' 计算利率
- PeriodicRate = (Rate / 100) / 12
- ' 计算期数
- Periods = NPer * 12
- ' 执行计算
- ActiveCell = "=PMT(" & PeriodicRate & "," + Periods & "," + PV & ",0,0)"
- Application.Calculation = xlCalculationAutomatic
- 'Application.Calculation = xlCalculationManual
- 'Application.ActiveWindow.ActiveCell.Calculate()
- End Sub
- ' 计算年金数量
- Sub CalculateFV(ByVal Rate As Double, ByVal NPer As Integer, ByVal PMT As Integer, ByVal PV As Integer)
- ' 计算利率
- PeriodicRate = (Rate / 100) / 12
- ' 计算期数
- Periods = NPer * 12
- ' 执行计算
- ActiveCell = "=FV(" + PeriodicRate & "," + Periods & "," + PMT & "," + PV & ",0)"
- Application.Calculation = xlCalculationAutomatic
- 'Application.Calculation = xlCalculationManual
- 'Application.ActiveWindow.ActiveCell.Calculate()
- End Sub
- ' 计算有效利率
- Sub CalculateEFFECT(ByVal Rate As Double)
- ' 计算利率
- PeriodicRate = Rate / 100
- ' 执行计算
- ActiveCell = "=EFFECT(" + PeriodicRate & ",12)"
- Application.Calculation = xlCalculationAutomatic
- 'Application.Calculation = xlCalculationManual
- 'Application.ActiveWindow.ActiveCell.Calculate()
- End Sub
复制代码 |
|