简体   繁体   English

VBA打开Excel文件,刷新Bloomberg数据,保存,关闭

[英]VBA to open Excel file, refresh bloomberg data, save, close

I'm trying to write a vba script that gets called in a batch file to open an excel file, refresh bloomberg data, save the file, and then quit excel. 我正在尝试编写一个在批处理文件中调用的vba脚本,以打开excel文件,刷新Bloomberg数据,保存文件,然后退出excel。
There was a historical question which asked something similar, but the suggested answer didn't seem to work - I can open the file and refresh the data, but it doesn't save the file or close excel. 有一个历史性的问题,它提出了类似的要求,但是建议的答案似乎不起作用-我可以打开文件并刷新数据,但是它无法保存文件或关闭excel。
I tried also putting in as a macro with the workbook_open file, but then ran into a problem where excel is saving and closing the file before refreshing the data. 我也尝试将workbook_open文件作为宏插入,但是遇到一个问题,即excel在刷新数据之前先保存并关闭文件。 Any suggestions would be much appreciated. 任何建议将不胜感激。
Immediately below is the modified vba code that refreshes the data, but doesn't save or close the excel workbook. 紧随其后的是修改后的vba代码,该代码刷新数据,但不保存或关闭excel工作簿。

 'Write Excel.xls  Sheet's full path here
 strPath = "C:\MngXL\testbook.xlsm" 

 'Create an Excel instance and set visibility of the instance
 Set objApp = CreateObject("Excel.Application") 
 objApp.Visible = True

 Set wbToRun = objApp.Workbooks.Open(strPath) 

 StartAutomation
 DoneNow

 Sub StartAutomation()
     Dim oAddin
     Set oAddin = objApp.Workbooks.Open("C:\blp\API\Office      Tools\BloombergUI.xla")

     If Not oAddin Is Nothing Then
         objApp.DisplayAlerts = False
         objApp.Calculate
         objApp.Run "RefreshAllStaticData"
         objApp.Calculate
         objApp.Run "RefreshAllStaticData"
        'WaitTillUpdateComplete
    End If

    dim count 
    dim updated 
    updated = false 
    for count = 0 to 12 
        if updated = false then
            if      objApp.WorksheetFunction.CountIf(objApp.Range("rng_inWorkbook"),"#N/A Requesting Data...") = 0      Then
                updated = true
            else
                Application.OnTime Now + TimeValue("00:00:15"),      WaitTillUpdateComplete
            end if
        end if
    next

 End Sub

 Private Sub WaitTillUpdateComplete()
     Dim t
    t = 0
    objApp.Calculate
     If      objApp.WorksheetFunction.CountIf(objApp.Range("rng_inWorkbook"),"#NAME?") > 0      Then
         Application.OnTime Now + TimeValue("00:00:15"),      "WaitTillUpdateComplete"
     ElseIf      objApp.WorksheetFunction.CountIf(objApp.Range("rng_inWorkbook"),"#N/A") > 0 Then
         Application.OnTime Now + TimeValue("00:00:15"),      "WaitTillUpdateComplete"
     ElseIf      objApp.WorksheetFunction.CountIf(objApp.Range("rng_inWorkbook"),"#N/A Requesting      Data...") > 0 Then
         If t < 5 Then
             t = t+ 1
             waitlonger
         Else
             Exit Sub
         End If
     Else
         Exit Sub
     End If

 End Sub

 Sub waitlonger()
     Dim x
     x = Now + TimeValue("00:00:40")
     Do While x > Now
     Loop
     objApp.Calculate
 End Sub

 Sub DoneNow()
    wbToRun.Save 
    wbToRun.Close
    objApp.DisplayAlerts = False 
    objApp.Quit 
    MsgBox strPath & " " & strMacro & " macro and .vbs successfully      completed!!!!", vbInformation
 End Sub

You need a strategy to let the refresh of Bloomberg data take about the right amount of time. 您需要一种策略来让彭博数据的刷新花费适当的时间。

Currently, your program seems to allow only certain small amounts of time to pass with no feedback. 当前,您的程序似乎只允许少量时间通过而没有反馈。 Instead, you need to make a loop that cycles once every 10 seconds (or whatever makes sense) and checks to see if the program is done. 相反,您需要制作一个循环,每10秒循环一次(或其他有意义的循环),并检查程序是否完成。

I like to do it this way: 我喜欢这样:

dim count as integer
dim updated as boolean

updated = false
for count = 1 to 12 'or any value you choose
   if updated = false then
      if objApp.WorksheetFunction.CountIf(objApp.Range("rng_inWorkbook"),"#NAME?") = 0 Then
         updated = true
      else
         Application.OnTime Now + TimeValue("00:00:15"), "WaitTillUpdateComplete"
      end if
   end if      
next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM