VBA Error Handling with ActiveWorkbook.RefreshAll

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I am writing a macro to refresh all queries in my workbook and want to handle errors during the refresh like this with something along the lines of:

Code:
On Error Resume Next

ActiveWorkBook.RefreshAll

If err.Number <> 0 Then

   Msgbox "Errors detected"

Else

   Msgbox "No errors"

End if

But this doesn't seem to work - even if an error is encountered, Err.Number returns 0.

On the other hand, if I refresh a connection known to have an error manually like this,

ActiveWorkBook.Connections("MyConnection").Refresh

Then the above error handling does work.

If I try to Refresh All manually (through the Excel ribbon), an error does appear - and then it prompts me if I want to continue ("One or more connections didn't refresh or the refresh operation was cancelled. Would you like to refresh the remaining connections").

What I really want is for VBA to detect that error and then cancel the remaining refreshes without the user needing to do it.

I would refresh to use Refresh All rather than looping through the queries but if that's only way then I guess it's not the end of the world.
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top