这是我编写VBA代码的第一次尝试,所以我知道它可能相当笨重,有些冗余。我试图设计一个按钮,根据每周的支付金额,确定偿还总债务需要多长时间。
我遇到的问题是尚未支付款项的贷款的应计利息。这就是我的电子表格的样子:
这是我的VBA代码,我试图引起对**的兴趣:
Private Sub CommandButton1_Click()
Dim x As Single
Dim y As Single
Dim z As Single
Dim Weeks As Integer
Dim Years As Single
Range("C3:H10").Copy Range("C13")
y = Range("H20")
x = Range("H13").Activate
Do While y > 0
x = ActiveCell - Range("A5")
**Range("H14").Value = Range("E14") * ((1 + Range("G14")) ^ Range("A3"))
Range("H15").Value = Range("E15") * ((1 + Range("G15")) ^ Range("A3"))
Range("H16").Value = Range("E16") * ((1 + Range("G16")) ^ Range("A3"))
Range("H17").Value = Range("E17") * ((1 + Range("G17")) ^ Range("A3"))
Range("H18").Value = Range("E18") * ((1 + Range("G18")) ^ Range("A3"))
Range("H19").Value = Range("E19") * ((1 + Range("G19")) ^ Range("A3"))**
If x < 0 And ActiveCell.Value = 0 Then
ElseIf x < 0 Then
ActiveCell.Value = 0
ActiveCell.Offset(0, -3).Value = 0
ActiveCell.Offset(1, 0).Activate
ActiveCell.Value = ActiveCell + x
x = ActiveCell
Else
ActiveCell.Value = x
ActiveCell.Offset(0, -3).Value = x
ActiveCell.Value = ActiveCell.Offset(0, -3).Value * ((ActiveCell.Offset(0, -1).Value + 1) ^ Range("A3"))
x = ActiveCell
End If
y = Range("H20")
Weeks = Weeks + 1
Loop
z = Range("H20")
Years = Round((Weeks / 52.14), 2)
Range("C13:H20").ClearContents
MsgBox ("It will take " & Years & " years to pay off your debt.")
Worksheets("Investment Income").Range("B7").Value = Years
Worksheets("Investment Income").Range("B8").Value = Abs(z)
End Sub
对此问题的任何想法和/或改进使代码更有效率?
答案 0 :(得分:0)
Excel具有强大的内置财务计算功能。您应该阅读这篇文章:Determining the Remaining Length of a Loan Using NPER
NPER([利率],[付款],[总贷款])