+ Reply to Thread
Results 1 to 6 of 6

Need tutoring!!! Writing a cumulative sum function (without calling vba function)

  1. #1
    Registered User
    Join Date
    09-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    3

    Unhappy Need tutoring!!! Writing a cumulative sum function (without calling vba function)

    Hi guys,
    I'm new to vba and recently I met a challenge in my work and I really appreciate if someone can give me some suggestions.

    So I'm trying to write a function to calculate a Bond's Price.
    Given values: coupon, maturity, yield.

    It will be something like this BondPrice=(coupon,maturity,yield)
    And this is the mathematics formula :

    Price (yield)= ∑_(i=1)^(maturity)▒〖CF〗_i/〖( 1+yield)〗^i

    note: 〖CF〗_i= (coupon)*100

    The price is a sum from period i = 1 to maturity, given coupon, maturity and yield.
    Specifically,
    BondPrice=P.1+P.2+P.3+...+P.maturity

    That sigma in the formula get me busted so hard because of the 'rule' we've given.

    Rule:
    Required to write the function without calling vba functions such as P=Sum(Range()). Yeah pure vba language!!!

    -------------------------------------------------------------------------------------
    Below is my 3-hours attempt and it wouldn't work as well as violated the rule.

    Public Function BondPrice(alpha, beta, gama)

    'alpha=coupon
    'beta=maturity
    'gama=yield

    Dim matrix(10 To 10, 1 To 100) As Double

    alpha = Cells(3, 3).Value
    beta = Cells(4, 3).Value
    gama = Cells(5, 3).Value

    'their values are gathered from a subroutine that I wrote before, I put them here for testing purpose.


    For i = 1 To beta

    matrix(10, i) = ((alpha ) * 100) / (1 + (gama ) ^ i)

    Next i

    BondPrice = Application.Sum(Range(matrix(10, 1), matrix(10, beta )))


    End Function

    ---------------------------------
    The work is due next Thursday. That could mean I have a lot of time to enjoy my frustration here.
    But I trust the community to be my salvation. Please help! And please don't forget the rule!
    Last edited by tliu1; 09-15-2014 at 12:01 AM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,933

    Re: Help!!! Writing a cumulative sum function (without calling vba function)

    Hi, welcome to the forum

    This sounds like a homework assignment?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    09-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    3

    Re: Help!!! Writing a cumulative sum function (without calling vba function)

    Yep.
    Any thoughts?

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,933

    Re: Help!!! Writing a cumulative sum function (without calling vba function)

    OK, just checking

    I see that you have already made some attempt at this, so that takes care of our "homework rule" ...

    Nothing is more frustrating than to be working on homework and to get stuck. Without guidance, you might have no hope of finishing the assignment before the deadline. The Internet can be a big help in that respect, but without learning how to get the right answer, asking others to solve your homework questions for you simply becomes a downward spiral (and it's cheating).

    We do not want to contribute to you cheating yourself out of your education, but we also acknowledge that seeking assistance to learn a concept is a legitimate request.

    If you are genuinely interested in receiving help in the form of tutoring or coaching, then please rephrase the title of your original post to clearly indicate you are seeking coaching or tutoring help. Any forum members (who are willing to assist as a tutor) will modify their responses accordingly to facilitate your learning. Tutors don't tell you the answers, they help you figure it out for yourself; so don't expect answers, expect suggestions, or just plain hints. Also, be specific in describing the function/formula or technique you trying to learn, and tell us what you have attempted so far. Otherwise, expect your plea for homework answers to be ignored.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: Need tutoring!!! Writing a cumulative sum function (without calling vba function)

    1. Why did you dimension your matrix with only 1 row with and index of ten, but 100 columns? Maybe just Dim Matrix(1 to 100) as Double?
    Why a matrix?
    Maybe:
    temp=(alpha * 100) / (1 + (gama ) ^ i)
    bondprice=bondprice + temp

    Is that: (1 + (gama ) ^ i) or (1 + gama ) ^ i
    Ben Van Johnson

  6. #6
    Registered User
    Join Date
    09-14-2014
    Location
    US
    MS-Off Ver
    2013
    Posts
    3

    Re: Need tutoring!!! Writing a cumulative sum function (without calling vba function)

    My appreciation!

    I tried:
    -------------------------------------------------
    Public Function BondPrice(alpha,beta,gama)

    For i = 1 To beta
    temp = (alpha * 100) / ((1 + gama) ^ i)
    BondPrice = BondPrice + temp
    Next i

    End Function
    ------------------------------------------------
    But when I loop this function in my subroutine I get all the cells with the same answer.
    --------------------------------------------
    Public Sub hw3

    'dim part neglected


    alpha=inputbox("coupon")
    beta=inputbox("maturity")
    gama=input box("yield")


    For i = 1 To 11
    Cells(i + 7, 3).Value = BondPrice(alpha, beta, gama)
    Next i
    End Sub
    --------------------------------------------------

    And I know what the problem is.
    In the function you suggested:

    BondPrice=BondPrice+temp

    This is a nice idea,
    but in order to get the sum I have to loop it.

    And when I loop it, the BondPrice gets reset to 0 again.

    That's why I came up with the idea of matrix because in matrix I can make it dynamic such as
    -------------------------------------------------------------
    For i = 1 To beta

    matrix(10, i) = ((alpha ) * 100) / (1 + (gama ) ^ i)

    Next i

    ------------------------------------------------------------
    matrix(10,1) will be p1, and matrix(10, 2) will be p2. But then I'll have to sum it up by function and that's a violation.

    10 in the matrix was nothing.

    Any thoughts?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Dax function for cumulative numbers/percents
    By Paul-NYS in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 08-26-2013, 08:50 AM
  2. Macro exits prematurely from function after calling other function
    By LouisPhilippe in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 03:22 AM
  3. [SOLVED] help with calling a function (B) and return control of program flow to the calling functio
    By john/nyc in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-26-2012, 11:06 AM
  4. Cumulative Count Function
    By larryg003 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-28-2010, 01:08 PM
  5. [SOLVED] How do I calculate the cumulative distribution function
    By macrohunter in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-17-2006, 01:45 PM

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