简体   繁体   中英

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.

Then a message box appears :

"unable to open 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.

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. 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 :

    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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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