一次运行多个子服务器

时间:2017-10-17 13:05:19

标签: excel vba excel-vba

我已经创建了一个工作簿,我将用它来调用我的其他工作簿及其宏。我有一个按钮“subap”分配给按钮,我会在需要更新时按下。但是,当我按下按钮并打开我的一个工作簿并调用宏时,vba不会等待该特定工作簿中的计算完成,但它会继续并开始打开下一个工作簿。我试图使用命令Application.wait,但似乎它不起作用。它会等待,但不会在后台同时计算。

Sub Knap()

Call LongerSeries
Application.Wait (Now + TimeValue("00:03:00"))
Call QuickCorrelation
Application.Wait (Now + TimeValue("00:03:00"))
Call SPX

End Sub

Sub LongerSeries()

Workbooks.Open ("G:\FONDS\Quick financials_Longer Series (2).xlsb")
Application.Run "RefreshData"

End Sub

Sub QuickCorrelation()

Workbooks.Open ("G:\FONDS\Quick correlation_1.xlsb")
Application.Run "RefreshData"

End Sub

Sub SPX()

Workbooks.Open ("G:\FONDS\Quick Intra Corr (SPX)_1.xlsb")
Application.Run "RefreshData"

End Sub

2 个答案:

答案 0 :(得分:0)

试试这样:

Sub LongerSeries()
    Workbooks.Open ("G:\FONDS\Quick financials_Longer Series (2).xlsb")
    Application.Calculate
    Application.Run "RefreshData"
End Sub

它会强制所有打开的工作簿中的碎片。 See more about Calculate here

答案 1 :(得分:0)

您尝试过使用数组吗?

Sub Knap()

Dim MyWorksheets()

MyWorksheets = Array("LongerSeries", "QuickCorrelation", "SPX")

For Each mw in MyWorksheets
    If mw = "LongerSeries" then
        Call LongerSeries
    ElseIf mw = "QuickCorrelation" Then
        Call QuickCorrelation
    ElseIf mw = "SPX" Then
        Call SPX
    Else
    End If        
Next    
End Sub

Sub LongerSeries()

Workbooks.Open ("G:\FONDS\Quick financials_Longer Series (2).xlsb")
Application.Run "RefreshData"
Application.Calculate
If Not Application.CalculationState = xlDone Then
    DoEvents
End If

End Sub

Sub QuickCorrelation()

Workbooks.Open ("G:\FONDS\Quick correlation_1.xlsb")
Application.Run "RefreshData"
If Not Application.CalculationState = xlDone Then
    DoEvents
End If

End Sub

Sub SPX()

Workbooks.Open ("G:\FONDS\Quick Intra Corr (SPX)_1.xlsb")
Application.Run "RefreshData"
If Not Application.CalculationState = xlDone Then
    DoEvents
End If

End Sub

希望这有帮助!