+ Reply to Thread
Results 1 to 5 of 5

Calling xirr in VBA

  1. #1
    Registered User
    Join Date
    08-19-2016
    Location
    New York
    MS-Off Ver
    2002
    Posts
    2

    Calling xirr in VBA

    I am trying to write a vba macro that calls xirr on an array constructed from arguments. However it always return 0. It works perfectly if I have a cell formula with xirr on the exact same data.

    A somewhat minimal version of the macro follows:
    Please Login or Register  to view this content.
    I am calling it on the following spreadsheet -
    1-Jan 1000
    1-Feb 100
    1-Mar 100 =myXirr(-B1,A1,B2,A2,30,2,B4,A4,0.1)
    1-Apr -1233.31 =Xirr(b1:b4,a1:a4)
    The Xirr cell correctly shows .127 but the myXirr always shows 0 regardless of its arguments (I know the 2 shouldn't be exactly the same in my simplified version with the 30 argument above, but they should be close enough).
    Thanks for any help.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,085

    Re: Calling xirr in VBA

    It is not easy to check without Excel example
    Using
    Please Login or Register  to view this content.
    the result is closed to 0 (2.980232239 / 1000 000 000)
    What should be the result ?
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Calling xirr in VBA

    Quote Originally Posted by coder7 View Post
    Please Login or Register  to view this content.
    [....]
    =myXirr(-B1,A1,B2,A2,30,2,B4,A4,0.1)
    =Xirr(b1:b4,a1:a4)
    [....]
    The Xirr cell correctly shows .127 but the myXirr always shows 0
    Actually, myXirr returns about 2.98E-09, which you would see if you format the cell as General or as Scientific.

    In part, this is due to a defect in the XIRR function: it always returns about 2.98E-09 if the first cash flow is zero.

    But the root cause of the problem is your ReDim statements. At the very least, they should be
    Please Login or Register  to view this content.
    Otherwise, the default first index is zero. Since you never set vals(0), it is zero, which stumbles over the aforementioned defect.

    However, with that correction, myXirr returns about 3.77038295269012, whereas XIRR returns about 0.127191597223282.

    Some other mistakes:

    1. You pass -B1 (-1000) to myXirr, but you pass B1 (1000) to XIRR. And you set vals(numInt+2)=-endVal (1233.31) in myXirr, whereas you pass -1233.31 in B4 to XIRR. In effect, your cash flows for myXirr are -1000, 100, 100, 1233.31, whereas your cash flows for XIRR are 1000, 100, 100, -1233.31.

    2. Also, by adding deltaDays (30) to B2, the third date in myXirr is 2-Mar instead of 1-Mar. Consequently, when you correct #1, myXirr returns about 0.127324062585831 instead of 0.127191597223282.

  4. #4
    Registered User
    Join Date
    08-19-2016
    Location
    New York
    MS-Off Ver
    2002
    Posts
    2

    Re: Calling xirr in VBA

    Thanks very much joeu. I hadn't realized that vba arrays start at 0.

  5. #5
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Calling xirr in VBA

    Quote Originally Posted by coder7 View Post
    I hadn't realized that vba arrays start at 0.
    By default. You can change that by putting Option Base 1 near the top of the module. But I prefer to specify the lower and upper bounds in the Dim and ReDim statements. When in doubt, use LBound and UBound to determine the limits of an array, at least during development.

+ 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. [SOLVED] XIRR vs. IRR
    By bruceisaacson in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-11-2014, 02:52 PM
  2. [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
  3. XIRR in VBA
    By atwy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2012, 05:33 AM
  4. XIRR and IRR
    By kevwags in forum Excel General
    Replies: 6
    Last Post: 02-09-2011, 03:59 PM
  5. [SOLVED] XIRR in VBA?
    By Don Wiss in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-10-2011, 03:02 PM
  6. XIRR: a bug?
    By gummy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-18-2007, 09:48 AM
  7. [SOLVED] To XIRR or Not To XIRR
    By Jonathan in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-17-2006, 10:20 AM
  8. XIRR in VBA
    By Tim Tabor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2005, 01:06 AM

Tags for this Thread

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