Hello People,
I am making an excel file to compare different sets of loans with eachother and calculate best alternatives given some parameters.
For allowing the calculations i need to make a table of total payments for the loan, already this is where things go wrong. For some reason i get rounding errors while making the table through VBA as compared to making the table in excel. The two pictures below show what happens:
In the first situation i calculate the table by using the sheet functions. Here are some more explanations about the formulas for each column:
A: just number 1 to infinity
B: Capital to be paid off at start of session (= Cell F from row -1)
C: Total annuity to be paid off (= cell J5 ; in this cell i used the function: =ABS(BET(J5;J3;J1)) to calculate this)
D: Intrest part to be paid off (= cell B of same row * cell (J5 ; in this cell i used the function: =((1+J4)^(1/12))-1 which calculates monthly intrest given a yearly intrest rate)
E: capital part to be paid off (= cell C of same row - cell D of same row)
F: Is rest capital after this period (= cell B of same row - cell E of same row) (this cell will be used in the next row)
loan-excelf.jpg
As you can see after 15 years (=180 monthly periods) the loan results to 0 where everything is calculated by excel ws
Below you can see the table that is derived from a VBA macro:
loan-vbaf.jpg
As you can see in period 180: the result is not 0 and I think this is because of rounding errors or not enough decimal spaces used in the VBA macro.
For generating this table, I actually did the same technique as described above in the ws functions, but now i am able to generate the table when n(=#years) increases (so if n=20 then there will be 240 monthly periods)
Below is the code i use to generate this table:
Sub HypothecaireLening()
Dim r As Integer 'rij
Dim k As Integer 'kolom
Dim m As Integer 'maand
Dim MaxMonth As Integer 'max aantal maanden te betalen
Dim MaxMonthOffs As Integer 'max aantal maanden + 2 (offset)
'worksheet activeren, moet nog aangepast worden
Worksheets("(A)").Activate
'al de cellen in range (A3 tot F1000 leegmaken)
Range("A3:F1000").ClearContents
'basisvariabelen ingeven
MaxMonth = Cells(3, 10).Value
MaxMonthOffs = MaxMonth + 2
m = 1
For r = 3 To MaxMonthOffs
Cells(r, 1).Value = m
Cells(r, 2).Value = Cells(r - 1, 6).Value
Cells(r, 3).Value = Cells(6, 10).Value
Cells(r, 4).Value = Cells(r, 2) * Cells(5, 10)
Cells(r, 5).Value = Cells(r, 3) - Cells(r, 4)
Cells(r, 6).Value = Cells(r, 2) - Cells(r, 5)
m = m + 1 'Bewerking van de volgende maand
Next r
End Sub
Is there anyone who understands my problem and has a solution for this problem?
I am aware that there may be faster calculation methods (maybe use range array instead of FOR LOOP?) for the VBA macro, but since i am only a beginner, i am already very happy that this works. I will finetune my code once i learn more about the effectiveness off VBA.
Thank you for your reaction
Bookmarks