2017-10-15 111 views
0

我正試圖自動化彭博社刷新。該過程分爲3個部分,循環運行。彭博公式刷新

a。 b。從數據庫複製代碼
b。刷新彭博
c。運行分析並將結果複製回數據庫並循環到步驟a直到最後一條記錄

從我在本網站和其他彭博相關討論中得到的問題是,彭博公式在宏運行時不會刷新。

如何破解我的代碼以便彭博社更新?

Sub Run_SplinterSequence() 

' 
'This macro automates the processing of a list of records. 
'When executed the list of records is splintered into individual rows which are then run through an analysis process. 
'The output results are then appended to the end of each record in the list. 
'The process will loop through each row of data in the list until all rows are processed. 
' 
'Macro by SD. 
' 

Dim ClipSnip As String 

Debug.Print "Running initial data preparation sequence..." & vbNewLine 

'Copy 'Raw_Data' data to 'Process_Data' tab 
Debug.Print "Transferring applicable data from 'Raw_Data' to 'Process_Data' tab..." 
    Application.ScreenUpdating = False 
    Sheets("Raw_Data").Select 
    Range("A1").Select 
    Selection.AutoFilter 
    ActiveSheet.Range("$A$1:$P$110").AutoFilter Field:=11, Criteria1:="GO" 
    Columns("B:P").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Sheets("Process_Data").Select 
    Range("A1").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

Debug.Print "Data transfer completed." & vbNewLine 


'Calculate total number of rows for splinter sequence 
Debug.Print "Calculating total volume for splinter sequence..." 

    Sheets("Process_Data").Select 
    Range("P2").Select 
    ActiveCell.FormulaR1C1 = "=COUNTA(C[-12])" 
    Range("P2").Select 
    Application.CutCopyMode = False 
    Selection.Copy 
    Range("Q2").Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

    Dim maxRowNum As Long 
    maxRowNum = Range("Q2").Value 

Debug.Print "-Data rows: " & maxRowNum & ", Record splinters: " & maxRowNum - 1 & "." & vbNewLine 

Debug.Print "Initiating Splinter Sequence..." & vbNewLine 

Call SplinterSplash 

'Transfer record splinter values + execute data analysis process. 

    Dim i As Long 
    Dim timeval As Date 

    Sheets("Splinter").Select 
    timeval = Range("K3").Value 


    For i = 2 To maxRowNum 

Debug.Print "'Control Panel' data transfer sequence initiated..." 
Debug.Print "Transferring data splinter: " & i - 1 & "..." 

    '1st Currency (Base currency (currency 1)) 
    Sheets("Process_Data").Select 
    Range("D" & i).Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("D" & i).Value) 
    Debug.Print "Process_Data Cell D" & i & " Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("control panel").Select 
     Range("D5").Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     ClipSnip = CStr(Range("D5").Value) 
     Debug.Print "control panel Cell D5 Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 

    '2nd Currency (Accounting currency (currency 2)) 
    Sheets("Process_Data").Select 
    Range("E" & i).Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("E" & i).Value) 
    Debug.Print "Process_Data Cell E" & i & " Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("control panel").Select 
     Range("D7").Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     ClipSnip = CStr(Range("D7").Value) 
     Debug.Print "control panel Cell D7 Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 

    'Currency of the Principal Amount 
    Sheets("Process_Data").Select 
    Range("F" & i).Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("F" & i).Value) 
    Debug.Print "Process_Data Cell F" & i & " Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("control panel").Select 
     Range("D13").Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     ClipSnip = CStr(Range("D13").Value) 
     Debug.Print "control panel Cell D13 Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 

    'Tenor 
    Sheets("Process_Data").Select 
    Range("H" & i).Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("H" & i).Value) 
    Debug.Print "Process_Data Cell H" & i & " Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("control panel").Select 
     Range("D15").Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     ClipSnip = CStr(Range("D15").Value) 
     Debug.Print "control panel Cell D15 Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 


    'Strike % (Set OTM strike x% away from Spot) 
    Sheets("Process_Data").Select 
    Range("I" & i).Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("I" & i).Value) 
    Debug.Print "Process_Data Cell I" & i & " Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("control panel").Select 
     Range("D17").Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     ClipSnip = CStr(Range("D17").Value) 
     Debug.Print "control panel Cell D17 Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" & vbNewLine 

    Sheets("bloomberg link").Select 
    Rows("39:39").Select 

    Application.Run "RefreshEntireWorksheet" 

    Debug.Print "Splinter transfer complete." & vbNewLine 

    Debug.Print "Initiating time delay sequence..." 
    Debug.Print "Pause delay set to " & timeval & ", Pause initiation time: " & Now() 

    Application.Wait (Now + TimeValue(timeval)) 

    Debug.Print "Pause terminated @: " & Now() & vbNewLine 

    'Tweaked here 

    Sheets("control panel").Select 
    Range("P16").Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("P16").Value) 
    Debug.Print "Control Panel Cell P16 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("Process_Data").Select 
     Range("M" & i).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

     ClipSnip = CStr(Range("M" & i).Value) 
     Debug.Print "Process_Data Cell M" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 

    Debug.Print "Initiating splinter analysis sequence..." 

    Call RunAnalysis 

    Calculate 

    'Auto calculation off 
    'Application.Calculation = xlManual 

    Debug.Print "Splinter analysis complete." & vbNewLine 

    Debug.Print "Transferring splinter: " & i - 1 & " output values to 'Process_Data' tab..." 
'output data fields 
    Sheets("control panel").Select 
    Range("P13").Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("P13").Value) 
    Debug.Print "Control Panel Cell P13 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("Process_Data").Select 
     Range("K" & i).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

     ClipSnip = CStr(Range("K" & i).Value) 
     Debug.Print "Process_Data Cell K" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 


    Sheets("control panel").Select 
    Range("Q13").Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("Q13").Value) 
    Debug.Print "Control Panel Cell Q13 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("Process_Data").Select 
     Range("L" & i).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

     ClipSnip = CStr(Range("L" & i).Value) 
     Debug.Print "Process_Data Cell L" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 


' Sheets("control panel").Select 
' Range("P16").Select 
' Application.CutCopyMode = False 
' Selection.Copy 
' 
' ClipSnip = CStr(Range("P16").Value) 
' Debug.Print "Control Panel Cell P16 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 
' 
'  Sheets("Process_Data").Select 
'  Range("M" & i).Select 
'  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
'   :=False, Transpose:=False 
' 
'  ClipSnip = CStr(Range("M" & i).Value) 
'  Debug.Print "Process_Data Cell M" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 


    Sheets("control panel").Select 
    Range("Q16").Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("Q16").Value) 
    Debug.Print "Control Panel Cell Q16 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("Process_Data").Select 
     Range("N" & i).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

     ClipSnip = CStr(Range("N" & i).Value) 
     Debug.Print "Process_Data Cell N" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" 


    Sheets("control panel").Select 
    Range("P18").Select 
    Application.CutCopyMode = False 
    Selection.Copy 

    ClipSnip = CStr(Range("P18").Value) 
    Debug.Print "Control Panel Cell P18 Val: '" & CStr(ClipSnip) & "' Copied to Clipboard" 

     Sheets("Process_Data").Select 
     Range("O" & i).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

     ClipSnip = CStr(Range("O" & i).Value) 
     Debug.Print "Process_Data Cell O" & i & " Val: '" & CStr(ClipSnip) & "' Pasted from Clipboard" & vbNewLine 


    'Auto calculation On 
    'Application.Calculation = xlAutomatic 

    Debug.Print "Splinter output transfer complete!" & vbNewLine 


    If i < maxRowNum Then 
     Debug.Print "Splinter sequence: " & i - 1 & " of: " & maxRowNum - 1 & " complete, looping to next splinter sequence..." & vbNewLine 
    Else 
     Debug.Print "Splinter sequence: " & i - 1 & " of: " & maxRowNum - 1 & " complete." & vbNewLine 
     Debug.Print "All splinter sequences completed successfully! ;D" & vbNewLine 

    End If 

    Next i 

End Sub 
+0

是否希望公式在**特定時間**或**特定位置**在執行代碼時刷新? –

回答

1

要做到彭博數據刷新(就像您手動按彭博插件按鈕),你需要知道哪些加載項的文件和子目錄的名稱。

從某處我忘了,谷歌,發現它是BloombergUI.xla!RefreshAllStaticData爲我所需要的。在調用之前確保AddIn在那裏。

一旦它被調用時,我給自己定下了一個「定時器」,然後再繼續檢查彭博的計算狀態(通過計算有多少#VALUE!)。這是我觀察和編碼的。

Private Const BRG_ADDIN As String = "BloombergUI.xla" 
Private Const BRG_REFRESH As String = "!RefreshAllStaticData" 
Private TimePassed As Integer 

Sub StartAutomation() 
    Dim oAddin As Workbook 
    On Error Resume Next 
    Set oAddin = Workbooks(BRG_ADDIN) 
    On Error GoTo 0 
    If Not oAddin Is Nothing Then 
     Application.Run BRG_ADDIN & BRG_REFRESH 
     StartTimer 
    End If 
End Sub 

Private Sub StartTimer() 
    TimePassed = 0 
    WaitTillUpdateComplete 
End Sub 

Sub WaitTillUpdateComplete() 
    If WorksheetFunction.CountIf(ThisWorkbook.Names("BloombergDataRange").RefersToRange, "#VALUE!") = 0 Then 
     Application.StatusBar = "Data update used " & TimePassed & "seconds, automation started at " & Now 
    Else 
     Application.StatusBar = "Waiting for Bloomberg Data to finish updating (" & TimePassed & " seconds)..." 
     TimePassed = TimePassed + 1 
     Application.OnTime Now + TimeSerial(0, 0, 1), "WaitTillUpdateComplete" 
    End If 
End Sub