=BDH() Formula shows as #N/A until after the Sub has finished running

j4ttlife

New Member
Joined
Feb 2, 2022
Messages
30
Platform
  1. Windows
  2. MacOS
I'm pulling in data from Bloomberg using '=BDH()' formula, however this shows as '#N/A Requesting Data...'. This formula is in the middle of the Subroutine. At the end I am trying to find the sum of values pulled in from Bloomberg using the =BDH formula.

My issue is that as the =BDH formula doesn't display the values until after the module has finished running, hence the =SUM function used at the very end shows a blank as no values are ready to be added within the time the macro runs.

I've tried a few solutions without luck such as 'Application.Wait', 'Application.ScreenUpdating' without luck.

Are there any ways I can refresh the =BDH values before the code advances to the =SUM part of my subroutine? A snip of the code in question is:

VBA Code:
Sub exampleCode()


'BDH formula used to pull in midprice. This stays as #N/A until after the entire module has run.
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To lastRow
Dim currency1 As String
currency1 = Range("A" & i).Value
    
Dim lastDayOfPrevMonth As Date
lastDayOfPrevMonth = DateSerial(Year(Date), Month(Date), 0)
    
    If currency1 = "AUD" Then
        Range("D" & i).Formula = "=BDH(""AUDUSD BGN Curncy"",""PX_MID"",""" & Format(lastDayOfPrevMonth, "mm/dd/yyyy") & """)"
    ElseIf currency1 = "CAD" Then
        Range("D" & i).Formula = "=BDH(""CADUSD BGN Curncy"",""PX_MID"",""" & Format(lastDayOfPrevMonth, "mm/dd/yyyy") & """)"
    ElseIf currency1 = "CHF" Then
        Range("D" & i).Formula = "=BDH(""CHFUSD BGN Curncy"",""PX_MID"",""" & Format(lastDayOfPrevMonth, "mm/dd/yyyy") & """)"

End if

Next i



'Sums the values pulled in the above If statements
TRow = WorksheetFunction.Match("Total", Range("A:A"), 0)

Dim lastRow1 As Long
Dim ix As Long

    lastRow1 = Cells(Rows.Count, "D").End(xlUp).Row
    
    For ix = 6 To lastRow1
    If Not IsEmpty(Range("C" & ix)) And Not IsEmpty(Range("D" & ix)) Then
        Range("E" & ix).Value = Range("C" & ix).Value * Range("D" & ix).Value
    End If
    
    Next ix

If GTRow > 0 Then
    Cells(GTRow, "E").Value = WorksheetFunction.Sum(Range("E6:E" & lastRow1))
End If


End Sub


What adjustments can I make to ensure the above =BDH formula shows the true value/refreshes before moving onto the Sum function?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
maybe try cycling calculations on and off?
 
Upvote 0

Forum statistics

Threads
1,215,248
Messages
6,123,869
Members
449,130
Latest member
lolasmith

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top