OnError... do it again up to 5 times then quit.

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hi Guys.

I've got some vba code that imports a website table onto a sheet. 99% of the time it works perfectly. 1% of the time the connection times out. I've found that if I manually trigger the exact same connection immediately it works. I've not used error handling before and have read up on forums about it... but still can't find what I need.

I'd like some code that will create a loop OnError 5 times before finally giving up... something like this:

Code:
dim ErrorCount as long


ErrorCount = 0


OnError start here again and ErrorCount = ErrorCount + 1

With Sheets("Sheet1").QueryTables.Add(Connection:= _
               "URL;http://websitewithtable.com" _
               , Destination:=Sheets("Sheet1").Range("$A$1"))
               .name = _
               "websitewithtable.com"
               .FieldNames = True
               .RowNumbers = False
               .FillAdjacentFormulas = False
               .PreserveFormatting = True
               .RefreshOnFileOpen = False
               .BackgroundQuery = True
               .RefreshStyle = xlInsertDeleteCells
               .SavePassword = False
               .SaveData = True
               .AdjustColumnWidth = True
               .RefreshPeriod = 0
               .WebSelectionType = xlEntirePage
               .WebFormatting = xlWebFormattingNone
               .WebPreFormattedTextToColumns = True
               .WebConsecutiveDelimitersAsOne = True
               .WebSingleBlockTextImport = False
               .WebDisableDateRecognition = False
               .WebDisableRedirections = False
               .Refresh BackgroundQuery:=False
           End With


if ErrorCount > 5 then

msgbox "I give up... the internet has obviously blown up!"

end if


OK... so that's sudo code but I hope it serves to illustrate my thinking.

I guess all I really need is a loop that works only in the event that there's an error.

As always, any help will be greatly appreciated :)
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code:
Sub importWebQuery()
Dim ErrorCount As Integer


On Error GoTo HandleError


With Sheets("Sheet1").QueryTables.Add(Connection:= _
               "URL;http://websitewithtable.com" _
               , Destination:=Sheets("Sheet1").Range("$A$1"))
               .Name = _
               "websitewithtable.com"
               .FieldNames = True
               .RowNumbers = False
               .FillAdjacentFormulas = False
               .PreserveFormatting = True
               .RefreshOnFileOpen = False
               .BackgroundQuery = True
               .RefreshStyle = xlInsertDeleteCells
               .SavePassword = False
               .SaveData = True
               .AdjustColumnWidth = True
               .RefreshPeriod = 0
               .WebSelectionType = xlEntirePage
               .WebFormatting = xlWebFormattingNone
               .WebPreFormattedTextToColumns = True
               .WebConsecutiveDelimitersAsOne = True
               .WebSingleBlockTextImport = False
               .WebDisableDateRecognition = False
               .WebDisableRedirections = False
               .Refresh BackgroundQuery:=False
End With


MainExit:
Exit Sub




HandleError:
ErrorCount = ErrorCount + 1
If ErrorCount > 5 Then
    MsgBox "I give up... the internet has obviously blown up!", vbExclamation
    Resume MainExit
Else
    Resume
End If


End Sub
 
Upvote 0
Thanks so much for this.

I'm testing now.

So this will make it count the errors and try the connection again 5 times before giving up? If so, that's perfect.
 
Upvote 0
Just tested this.

It seems to go to the error handler and count to 5 then quit.

What I need is for it to repeat the data query 5 times (only if errors occur in the process) before it quits. Can you help me to make this loop the data query and count?
 
Upvote 0
an error does occur for your webquery because that website does not appear to exist, so it tries to import it 5 times and since it always fails it then just quits, test it with a link that works
 
Upvote 0
Thanks VBA Geek.

The website I the example is just a dummy site - so yes it doesn't exist... I wasn't expecting anyone to test with the dummy site.

I still need a solution that loops on error and then proceeds as normal if the web query works... or gives up after 5 tries.
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,263
Members
448,881
Latest member
Faxgirl

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