【发布时间】:2018-05-14 21:08:30
【问题描述】:
我已尝试收集我可以完成的所有代码,但它仍然不适合我。 我想要做的是安排我的 Excel 文件的任务,我有代码“RunExcel.vbs”作为附件,但仍然无法正常工作。
参考链接:How to set recurring schedule for xlsm file using Windows Task Scheduler
参考链接:https://www.mrexcel.com/forum/excel-questions/794869-vb-script-refresh-bloomberg-feed-excel.html
- 打开“TEst 文件夹”中的文件“PriceRealTIme.xlsm”(启用宏的工作簿)。
- 忽略更新链接
- 让它“刷新 Bloomberg 数据”并“等待 1 分钟或直到刷新完成”。
- 一旦完成。我想使用名为“CopyPaste”的宏复制这些列的粘贴值。
- 最后,让它“保存”和“关闭”文件。
' a .vbs file is just a text file containing visual basic code that has the extension renamed from .txt to .vbs 'Write Excel.xls Sheet's full path here strPath = "C:\Users\chaic\OneDrive\Desktop\TEst\PriceRealTIme.xlsm" 'Write the macro name - could try including module name strMacro = "Sheet1.CopyPaste" 'Create an Excel instance and set visibility of the instance Set objApp = CreateObject("Excel.Application") objApp.Visible = True ' or False 'Open workbook; Run Bloomberg Addin; Run Macro; Save Workbook with changes; Close; Quit Excel Set wbToRun = objApp.Workbooks.Open(strPath) 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 = 10 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 objApp.Run strMacro ' wbToRun.Name & "!" & strMacro wbToRun.Save wbToRun.Close objApp.Quit 'Leaves an onscreen message! MsgBox strPath & " " & strMacro & " macro and .vbs successfully completed!", vbInformation
【问题讨论】:
-
更正等待时间我只需要 10 秒让 Bloomberg 刷新数据。
-
您只需要等待 1 秒,彭博就会在此行中刷新
Application.OnTime Now + TimeSerial(0, 0, 1), "WaitTillUpdateComplete".您在此功能中已过一秒TimeSerial( hour, minute, second )。我认为您需要传递TimePassed变量来代替1 -
我必须传递 TImepassed 变量来代替 1 是什么意思?谢谢。
-
您每秒钟都在调用 WaitTillUpdateComplete sub。彭博需要更多时间来刷新。
标签: excel vba vbscript schedule bloomberg