473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

I'm trying to include the Excel MDuration function into VBA code in an Access Module.

I'm trying to include the Excel MDuration function into VBA code in an Access Module. Is this possible?

My goal is to calculate the Modified Duration of a bond using the key inputs and have the duration calculation sent to a table.

The code works up until the line "Duration = "=MDURATION(SettlementDate,MaturityDate,Coupon,Yie ld,Frequency)"

Where I'm I going wrong or is this simply not possible?

My code is as follows.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function CalcDuration()
  3.  
  4. Dim SettlementDate As Date
  5. Dim MaturityDate As Date
  6. Dim Coupon As Double
  7. Dim Yield As Double
  8. Dim Duration As Double
  9. Dim Frequency As Double
  10.  
  11.  
  12. SettlementDate = InputBox("Enter Settlement Date ", "Enter Value")
  13. MaturityDate = InputBox("Enter Maturity Date ", "Enter Value")
  14. Coupon = InputBox("Enter Coupon ", "Enter Value")
  15. Yield = InputBox("Enter Yield ", "Enter Value")
  16. Frequency = InputBox("Enter Frequency ", "Enter Value")
  17. Duration = "=MDURATION(SettlementDate,MaturityDate,Coupon,Yield,Frequency)"
  18.  
  19.  
  20. CurrentDb.Execute "INSERT INTO Duration" _
  21.     & "(SettlementDate, Duration) VALUES " _
  22.     & "(#" & SettlementDate & "#, " & Duration & ")"
  23.  
  24.  
  25. End Function
  26.  
  27.  
Nov 10 '16 #1

✓ answered by ADezii

The following Code is fully operational, I am just not sure that it is producing the desired results. That's your area of expertise (LOL).
Expand|Select|Wrap|Line Numbers
  1. Dim dteSettlementDate As Date
  2. Dim dteMaturityDate As Date
  3. Dim sngCouponRate As Single
  4. Dim sngAnnualYield As Single
  5. Dim bytFrequency As Byte
  6.  
  7. dteSettlementDate = #4/1/2005#
  8. dteMaturityDate = #3/31/2025#
  9. sngCouponRate = 0.08
  10. sngAnnualYield = 0.04
  11. bytFrequency = 4
  12.  
  13.  
  14. Dim appExcel As Excel.Application
  15.  
  16. Set appExcel = New Excel.Application
  17.  
  18. Debug.Print "Duration (Yrs.): " & appExcel.WorksheetFunction.MDuration(dteSettlementDate, dteMaturityDate, sngCouponRate, sngAnnualYield, bytFrequency)
  19.  
  20. appExcel.Quit
  21. Set appExcel = Nothing
Expand|Select|Wrap|Line Numbers
  1. Duration (Yrs.): 11.948498418378

8 1187
jforbes
1,107 Expert 1GB
I don't do what your attempting to do, but this seems to work:
Expand|Select|Wrap|Line Numbers
  1. Duration = Excel.WorksheetFunction.MDuration(SettlementDate,MaturityDate,Coupon,Yield,Frequency)
I have a feeling that it might be better to create an instance of Excel in a Variable, use that variable instead of calling Excel directly, and then set the Variable to Nothing so that Excel gets closed correctly.
Nov 10 '16 #2
ADezii
8,834 Expert 8TB
The following Code is fully operational, I am just not sure that it is producing the desired results. That's your area of expertise (LOL).
Expand|Select|Wrap|Line Numbers
  1. Dim dteSettlementDate As Date
  2. Dim dteMaturityDate As Date
  3. Dim sngCouponRate As Single
  4. Dim sngAnnualYield As Single
  5. Dim bytFrequency As Byte
  6.  
  7. dteSettlementDate = #4/1/2005#
  8. dteMaturityDate = #3/31/2025#
  9. sngCouponRate = 0.08
  10. sngAnnualYield = 0.04
  11. bytFrequency = 4
  12.  
  13.  
  14. Dim appExcel As Excel.Application
  15.  
  16. Set appExcel = New Excel.Application
  17.  
  18. Debug.Print "Duration (Yrs.): " & appExcel.WorksheetFunction.MDuration(dteSettlementDate, dteMaturityDate, sngCouponRate, sngAnnualYield, bytFrequency)
  19.  
  20. appExcel.Quit
  21. Set appExcel = Nothing
Expand|Select|Wrap|Line Numbers
  1. Duration (Yrs.): 11.948498418378
Nov 10 '16 #3
Thanks for the help #jforbes. It doesn't seem to work for me. It's saying "Run-time error '424': Object required".
Nov 14 '16 #4
Thanks for the help #ADezii. Also having issues here: it says "Compile error: User-defined type not defined" and it stops at the "Dim appexcel As Excel.Application" line.

I think I've found a solution that seems to be working. See below.
Nov 14 '16 #5
Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Function CalcDuration()
  3.  
  4.     Dim x1 As Object
  5.     Set x1 = CreateObject("Excel.Application")
  6.  
  7.     Dim SettlementDate As Date
  8.     Dim MaturityDate As Date
  9.     Dim Coupon As Double
  10.     Dim Yield As Double
  11.     Dim Duration As Double
  12.     Dim Frequency As Double
  13.  
  14.  
  15.     SettlementDate = InputBox("Enter Settlement Date ", "Enter Value")
  16.     MaturityDate = InputBox("Enter Maturity Date ", "Enter Value")
  17.     Coupon = InputBox("Enter Coupon ", "Enter Value")
  18.     Yield = InputBox("Enter Yield ", "Enter Value")
  19.     Frequency = InputBox("Enter Frequency ", "Enter Value")
  20.     Duration = x1.MDURATION(SettlementDate, MaturityDate, Coupon, Yield, Frequency)
  21.  
  22.  
  23.     CurrentDb.Execute "INSERT INTO Duration" _
  24.         & "(SettlementDate, Duration) VALUES " _
  25.         & "(#" & SettlementDate & "#, " & Duration & ")"
  26.  
  27.  
  28.  
  29. End Function
  30.  
  31.  
Nov 14 '16 #6
ADezii
8,834 Expert 8TB
"Compile error: User-defined type not defined" and it stops at the "Dim appexcel As Excel.Application"
The reason for this Error is because I use Early Binding while you use Late Binding. If you set a Reference to the Microsoft Excel XX.X Object Library, this Error will not appear.
Nov 14 '16 #7
Ah ok great, big thanks #ADezii
Nov 14 '16 #8
ADezii
8,834 Expert 8TB
You are quite welcome.
Nov 14 '16 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Oci-One Kanubi | last post by:
Everything works fine in Access, but when I double-click on the resultant Excel files the first one opens correctly, but subsequent ones, and any other Excel files I try to open, fail to display at...
17
by: Pam Ammond | last post by:
I need to use Microsoft Access Automation within a Visual Studio 2003 program written in C# for Windows Forms. When a button is clicked in my VS.NET program, I want it to run a Microsoft Access...
2
by: tonytony24 | last post by:
Hi All: I was wondering if there's a simple way to call a MS Access Module through either Command Prompt MS Script any other way... Thanks for the response.
2
by: LittlePhil via AccessMonster.com | last post by:
Someone please help before i start to cry. I'm trying to export from Access to Excel, then create a new excel sheet with a pivot table to display the data held in columns A:P. I get the error...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
2
by: Cele Balser | last post by:
Hello, This can't be that hard! Could someone give me the code to run a macro in my Personal.xls from my Access module? I have created a query in Access, exported it to a network drive, I can open...
1
MitchR
by: MitchR | last post by:
Good Morning Folks; I have a question that is pretty far fetched but here goes nothing... I am looking to find a way to insert a macro into an Excel command button located in an Access VBA...
3
by: keirnus | last post by:
Hello once again... I made a function in Excel. The function does some error checking within the Excel file. To be easy for me, I want my code in MS Access to simply call the function in Excel....
0
by: titli | last post by:
I have created the excel worksheet from MS Access using the following code: Dim xlApp As Excel.Application Dim xlWB As Excel.Workbook Set xlApp = New Excel.Application With xlApp ...
1
by: Piers | last post by:
I use this code to create some excel files from my DB, I would like a process that will send each indivdual file to a specific e-amil address, I will hold the e-mais in a table linked to the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.