简体   繁体   English

Excel VBA如何在出现消息框时自动接受

[英]Excel VBA how to automatically accept IF a message box appears

I am running a macro which refreshes imported data each minute. 我正在运行一个宏,每分钟刷新一次导入的数据。 The data is drawn from a URL however sometimes it is unable to open it due to poor connection. 数据是从URL提取的,但是有时由于连接不良而无法打开。

Then a message box appears : 然后出现一个消息框:

"unable to open http:..." “无法打开http:...”

and I have to click OK in order for it to continue. 而且我必须单击“确定”才能继续。

It does not always do this as mostly it can access the data but as this needs to be running throughout the day in the background I can't be watching it 24/7 to click OK and keep it going. 它并不总是这样做,因为大多数情况下它可以访问数据,但是由于它需要在后台全天运行,因此我无法24/7全天候观看它以单击“确定”并保持运行。

Is there anyway I can edit the code to include something like : 无论如何,我可以编辑代码以包括以下内容:

If MsgBx appears
Sendkeys ~ (enter)

I have tried DisplayAlerts = False which doesn't work. 我试过了DisplayAlerts = False这是行不通的。 And there is no way to improve the connection to prevent error from occuring in first place. 并且没有改善连接的方法来防止错误的发生。

If it can't access the data I just want it to leave it, move on, and try again in the next minute batch. 如果它无法访问数据,我只希望它保留它,继续前进,然后在下一个分钟内重试。

Have spent ages searching these forums and have not found the answer anywhere. 花了很长时间搜索这些论坛,却找不到任何答案。 Any ideas much appreciated. 任何想法表示赞赏。 Thanks 谢谢

You should try to trap the error directly by using error handling : 您应该尝试通过使用错误处理直接捕获错误:

Place this line at the top of your code : 将此行放在代码的顶部:

On Error GoTo ErrHandler

And these at the end, right before the End Sub : 这些在最后,在End Sub之前:

    Exit Sub 
ErrHandler: 
    Select Case Err.Number 
    Case Is <> "1004" 
        MsgBox "Error number - " & Err.Number & vbCrLf & Err.Description 
    Case Else 
        Application.Wait Now + TimeValue("00:00:05") 
        Resume 
        Goto Retry 
    End Select 

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

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