我正在尝试创建一个摊销表,其中利率取决于用户提供的两个输入。
X 代表行,Y 代表列。X、Y 和利率的值已经设置在 4 X 6 表中。例如,如果用户输入 X=2 和 Y=3,那么利率将被确定为 5%。
IF 函数可以工作,但它会占用大量时间并且效率不高。
我考虑过使用数组,我认为 Vlookup 将是最有效的。在 Excel 中,我将 Vlookup 与 Match 一起使用,它可以工作,但我很难将其转换为 VBA 代码。
Option Explicit
Sub Amortisation()
Dim intRate, loanLife, initLoanAmt
Dim yrBegBal, intComp, prinComp, yrEndBal, annualPmt
Dim outRow, rowNum, outsheet
outRow = 3 'output table begins from row 4
outsheet = "loan amort"
Worksheets(outsheet).ActivateDo
loanLife = InputBox("Enter loan life." _
& " Loan life must be a whole number")
If loanLife < 0 Or (loanLife - Round(loanLife) <> 0) Then
MsgBox ("Loan life must be a whole number.")
End
End If
initLoanAmt = InputBox("Enter loan amount." _
& " Loan amount must be a positive whole number")
If initLoanAmt < 0 Or (initLoanAmt - Round(initLoanAmt) <> 0) Then
MsgBox ("Loan amount must be a positive whole number.")
End
End If
End Sub
我希望 VBA 使用给定的输入从下表中选择利率,而不是像我对其他输入所做的那样提示利率。
因此,如果 X(贷款期限)为 5,Y(initloanamount)为 700,那么我希望 VBA 使用 10 作为利率。
在此之后,我可以使用 PMT 函数继续使用摊销表。