This is my first code ive written in vba so I apologise in advance...
My problem is this...
I have created a macro that calculates mduration, it take the relevent inputs and plugs it into the macro version of mduration (a function). The macro runs fine all the way through. However, it takes about 13mins to complete!!!
I have tried everything that I could think of but alas my knowledge of vba is not good enough.
My question is:
Can anyone help me find a way to reduce this speed, in an ideal world to under a minute.
IMPORTANT THINGS TO BE AWARE OF:
- The this macro runs on 130,000 rows!
- The only way i could think to create this macro is with a loop (suggestions welcome)
I have used alot of "offset" in my coding so the actual columns are the following;
AD = where i want the results to appear
AK = coupon
K = NAV
E = Nominal
AB = Mdate
Q = asset type
AC = maturity
Once again this is my first code and first post so its not perfect.
Thank you!!!!
Sub mduration()
Dim mduration As Variant
Dim yield As Single
Dim coupon As Single
Dim nav As Single
Dim nominal As Single
Dim mdate As Date
Dim mdateround As Date
Dim asset As String
Dim maturity As Double
Dim mdurationround As Variant
'to speed up sheet calculation: mdowsett 04/11/2010
Application.DisplayAlerts = False
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
'change to correct column (Mduration) and first row: mdowsett 04/11/2010
Range("ad3").Select
Do
'Only bonds etc remaing: mdowsett 05/11/2010
If ActiveCell.Offset(0, -13).Value <> "Bond" And ActiveCell.Offset(0, -13).Value <> "Non-concerned Bond" And ActiveCell.Offset(0, -13).Value <> "Non-EEA gvt" And ActiveCell.Offset(0, -13).Value <> "Non-concerned bond" Then
ActiveCell.Value = ""
'if nav has no value
ElseIf ActiveCell.Offset(0, -19).Value = 0 Then ActiveCell.Value = "not applicable"
'if term to maturity has no value
ElseIf ActiveCell.Offset(0, -1).Value = 0 Then ActiveCell.Value = 0
'if maturity date has no value: 05/11/2010
ElseIf ActiveCell.Offset(0, -2).Value = "" Then
'calculation for parts of mduration: mdowsett 04/11/2010
maturity = ActiveCell.Offset(0, -1).Value
mdateround = Round((40178 + (maturity * 365)), 0)
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'muduration with a missing maturity date: 05/11/2010
mdurationround = Application.WorksheetFunction.mduration("12/31/2009", mdateround, coupon, yield, 1, 1)
ActiveCell.Value = mdurationround
ElseIf ActiveCell.Offset(0, -2).Value <> "" Then
maturity = ActiveCell.Offset(0, -1).Value
mdate = ActiveCell.Offset(0, -2).Value
nominal = ActiveCell.Offset(0, -25).Value
nav = ActiveCell.Offset(0, -19).Value
coupon = (ActiveCell.Offset(0, 7).Value / 100)
yield = coupon / (nav / nominal)
'mduration with maturity date: 05/11/2010
mduration = Application.WorksheetFunction.mduration("12/31/2009", mdate, coupon, yield, 1, 1)
ActiveCell.Value = mduration
End If
ActiveCell.Offset(1, 0).Select
'until asset type has no value: 05/11/2010
Loop Until ActiveCell.Offset(0, -13).Value = 0
'turn calculations back on: mdowsett 04/11/2010
Application.Calculation = xlCalculateAutomatic
Application.DisplayAlerts = True
Application.ScreenUpdating = True
msgbox "Completed", 0, "MDuration"
End Sub
Display More