Results 1 to 13 of 13

VBA error in calculations. Need more decimals?

Threaded View

  1. #1
    Registered User
    Join Date
    11-14-2014
    Location
    Belgium
    MS-Off Ver
    MS 360
    Posts
    9

    VBA error in calculations. Need more decimals?

    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
    Attached Images Attached Images
    Last edited by resolate; 01-22-2017 at 11:29 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Listbox item not rounding to 2 decimals - Runtime error 13 Type mismatch
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-20-2016, 09:18 AM
  2. Forumula is adding 10+ Decimals during calculations
    By sapheri in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 2
    Last Post: 06-23-2015, 11:40 AM
  3. Forumula is adding 10+ Decimals during calculations
    By sapheri in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-23-2015, 11:08 AM
  4. Replies: 2
    Last Post: 02-13-2015, 07:36 AM
  5. Calculations with increasing decimals shown
    By tpampel in forum Excel General
    Replies: 1
    Last Post: 05-01-2007, 06:12 PM
  6. Error with decimals
    By Leamsi in forum Excel General
    Replies: 2
    Last Post: 05-18-2006, 03:10 PM
  7. Calculations Error.
    By bchowdhury in forum Excel General
    Replies: 3
    Last Post: 05-05-2005, 10:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1