I am trying to pull interpolated treasury yields through a spreadsheet using Bloomberg formulas. The date in cell D2 has to change then the interpolated yields for each day in 10 years future populate in M4:M2612.
The spreadsheet calculates these interpolated yields from a table that is present in the spreadsheet.
After speaking with Bloomberg help desk, I am trying to use the Application.OnTime to allow the code to load before changing the next date. The code was running in an infinite loop through every date (still without loading the yield data properly). I added the "If Day = EndDate then Exit For" and now it runs in an infinite loop between the first two dates included in my loop.
It gives no errors in the Debug in Excel. Any suggestions on how to pull this data otherwise?
I need this code to pull every day for 12 years, I am using a smaller date range to sample the code.
Public Sub master()
Call Range("A1:A2609").ClearContents
Call Range("M4:M2612").Select
Call Application.Run("RefreshCurrentSelection")
Call Application.OnTime(Now + TimeValue("00:00:02"), "Master2")
End Sub
Sub Master2()
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim c As Range
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Sheet1")
Set sht2 = wb.Sheets("Sheet2")
Dim StartDate As Date
Dim EndDate As Date
Dim Day As Date
StartDate = #4/2/2007#
EndDate = #4/6/2007#
For Day = StartDate To EndDate
MsgBox (Day)
If Day = EndDate then Exit For
sht1.Range("D2").Value = Day
For Each c In Selection.Cells
If c.Value = "#N/A Invalid Parameter:Interpolation Values" Then
Call Application.OnTime(Now + TimeValue("00:00:02"), "Master2")
Exit Sub
End If
Next c
sht2.Range("A1:A2609").Offset(1, 1).Value = sht1.Range("M4:M2612").Value
Next Day
End Sub
I was looking for something else and stumbled on this question, and I think I got why this is on an infinite loop.
Lets understand the code. Here is the first part:
Public Sub master()
Call Range("A1:A2609").ClearContents
This clears the range A1:A2609. Would recommend to get it dynamically
Call Range("M4:M2612").Select
Selects the range to be updated by bloomberg. Also try to change it to get dynamically.
Call Application.Run("RefreshCurrentSelection")
Runs the code to update from bloomberg
Call Application.OnTime(Now + TimeValue("00:00:02"), "Master2")
Runs the next sub in 2 seconds
End Sub
No loops locked here, so this is ok. The bloomberg part is kinda new to me, but some google got me the answers
Now, the second sub. Since the first one didn't loop, the offender would be here.
Sub Master2()
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim c As Range
Until here, just defining variables
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Sheet1")
Set sht2 = wb.Sheets("Sheet2")
Here everything is ok too
Dim StartDate As Date
Dim EndDate As Date
Dim Day As Date
As @Kyle mentioned, avoid using Day, because vba already have something with this name.
StartDate = #4/2/2007#
EndDate = #4/6/2007#
Defining variables value, so ok too.
For Day = StartDate To EndDate
MsgBox (Day)
Msgbox for test, you may want to remove this later. This will always trigger for every day.
If Day = EndDate then Exit For
You don't need this. You could just change the EndDate
variable to one day less.
sht1.Range("D2").Value = Day
Writes the day on D2
. This will always be the last day it used.
For Each c In Selection.Cells
Do the following code for every selected cell
If c.Value = "#N/A Invalid Parameter:Interpolation Values" Then
If its an error
Call Application.OnTime(Now + TimeValue("00:00:02"), "Master2")
Exit Sub
Start the code again in 2 seconds and exit this code. And here is your loop! If it has an error, it will start over in 2 seconds. It gets on an infinite loop on the first two dates because the second date will return "#N/A Invalid Parameter:Interpolation Values"
The rest ot the code, just for completeness
End If
Next c
sht2.Range("A1:A2609").Offset(1, 1).Value = sht1.Range("M4:M2612").Value
Next Day
End Sub
And that's it. Sometimes you have to re-read your code from the beginning. Just imagine you are explaining everything your code do to someone who has no understanding of code.
As I mentioned, I recommend getting the ranges dynamically, so if the specifications change, your code is ready for it and there is no need to tinker.
I know this is an old question, but if someone stumbles here as I did, here is an explanation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.