VBA在没有代码的情况下停止告诉它这样做

时间:2017-06-02 13:40:01

标签: vba

几乎是标题。代码前进直到它到达一行,然后行为就像它遇到Stop命令一样,除了没有这样的行。

StartDate = DateAdd("d", -1 * NDays + 1, EndDate) 'the immediately preceding line
'NDays is an integer, EndDate is a date

With Excel.Application 'the line that it stops on
    .ScreenUpdating = True
    .DisplayAlerts = True
    .Calculation = xlAutomatic
End With 'I put these here because the next lines save the workbook
    'if I put them before the excel.application stuff it asks if I want to save when closing

为什么它停在第二行?有人知道吗?

1 个答案:

答案 0 :(得分:0)

尝试不使用with块,看看会发生什么。如下所示:

Application.Screenupdating = True
Application.DisplayAlerts = False
Application.Calculation = xlCalculationAutomatic

我为为什么而感到茫然(如果有人知道,请分享),但我遇到了实例化新应用程序的问题,因此代码展示了同样的问题行为,它使用新实例,只是关闭。

如果我来冒险猜测,我会认为它与Excel管理VBA流程的方式有关。有可能(虽然我可能完全错误)新的应用程序进程采取控制,并且这导致第一个应用程序(以外行人的术语)假设它已完成,并且只是退出。那,或者它暂停自己的进程(如DoEvents命令),但不能恢复。再次,这些都是猜测。

另外,为什么' Excel.Application'?您是否正在创建名为Excel的新实例?如果您打算仅使用该应用程序,则代码只会在我们的“应用程序”中运行。