Consulting

Results 1 to 8 of 8

Thread: Using String variables within a formula in VBA

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    16
    Location

    Red face Using String variables within a formula in VBA

    Hi,

    I'm trying to code the user-defined function calculating the forward rates.
    I do not understand why this code doesn't work:


    Function ForwardRate(freq As Integer, Date1 As String, Date2 As String, Rate1 As Double, Rate2 As Double) As Double
    
    
         ForwardRate = (((1 + Rate2 / freq) ^ (freq * Date2) / (1 + Rate1 / freq) ^ (freq * Date1) ^ (1 / (freq * (Date2 - Date1)))) - 1) * freq
    
    
    End Function
    Should I declare Date1 and 2 as double, which is not in date format, but t=1.5, 2 , 2.5.. etc.
    I did, but it is still displaying as N/A

    Regards,
    Demian
    Attached Files Attached Files
    Last edited by demian; 09-29-2017 at 10:59 AM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Works fine for me declared as double, well it returns a decimal answer at least.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2017
    Posts
    16
    Location
    I am sorry to bother you, I am very new to VBA.
    I am still running into difficulties for the following code.


    Function ForwardRate(freq As Integer, Date1 As Double, Date2 As Double, Rate1 As Double, Rate2 As Double) As Double
    
    
     ForwardRate = (((1 + Rate2 / freq) ^ (freq * Date2) / (1 + Rate1 / freq) ^ (freq * Date1) ^ (1 / (freq * (Date2 - Date1)))) - 1) * freq
     
    
    
    End Function
    It looks like I am missing the very important concept
    Could you please correct me?

    Regards,
    Demian
    Attached Files Attached Files

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I cannot get the function to work in your workbook. It works if I change the name (MyRate) or use it in a new workbook. I guess some corruption.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It's odd, it keeps showing #NAME. If I change the function to FRate, or ForRate, and change the calls on the sheet, it works fine. It just doesn't like ForwardRate.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Sep 2017
    Posts
    16
    Location
    Yes, it's very odd.
    I created the new workbook to change the name of user-defined function per your advice.
    Now it works well.
    Thank you very much for all your wise advice and Sorry for bugging you with corrupted file.

    Regards,
    Demian

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Don't use the same name for a module and a function, sub, or other Excel entity


    Capture.JPG
    Last edited by Paul_Hossler; 09-29-2017 at 03:14 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Sep 2017
    Posts
    16
    Location
    Got it, thanks again

Posting Permissions

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