How Can I stop Excel from Crashing When downloading a page from the Internet?

StevenIramus

New Member
Joined
Sep 2, 2014
Messages
3
I am using excel and VBA to download a number of web pages and parse data from those webpages. I have my code set up but on occasion (every several hundred pages) the application crashes because it can't get a response from the internet.

My question is can I add code that essentially instructs Excel to wait ~5 seconds for a page to load. If the page doesn't load cancel and retry? Here is my code:

Sub Macro2()
'
' Macro2 Macro
' Macro recorded 8/30/2014 by [Redacted]
'
Dim StrPath As String
StrPath = "[Redacted]"
Application.ScreenUpdating = False
y = ThisWorkbook.Sheets("Performance").Range("A45").Value + 1
For x = y To 2008000000
Workbooks.Open (StrPath & x)
Workbooks("Performance.asp").Worksheets("Performance").Range("A1:AS22").Copy
ThisWorkbook.Sheets("Performance").Range("A1:AS22").PasteSpecial
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
ThisWorkbook.Sheets("Performance").Range("A45").Value = x
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "1" Then
ThisWorkbook.Sheets("Position").Rows("3:3").Insert Shift:=xlDown
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "1" Then
ThisWorkbook.Sheets("Performance").Range("A67:EQ67").Copy
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "1" Then
ThisWorkbook.Worksheets("Position").Range("A3:EQ3").PasteSpecial
Paste:=xlPasteValues
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "2" Then
ThisWorkbook.Sheets("Pitch").Rows("3:3").Insert Shift:=xlDown
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "2" Then
ThisWorkbook.Sheets("Performance").Range("A70:FF70").Copy
If ThisWorkbook.Sheets("Performance").Range("B42").Value = "2" Then
ThisWorkbook.Worksheets("Pitch").Range("A3:FF3").PasteSpecial
Paste:=xlPasteValues
Application.CutCopyMode = False
Workbooks("Performance.asp").Close SaveChanges:=False
ThisWorkbook.Sheets("Performance").Range("A1:AS22").ClearContents
If Int(x / 100) = x / 100 Then ThisWorkbook.Save
Next x
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You could try an On Error handler around the Workbooks.Open statement. Replace that line with:
Code:
    On Error Resume Next
    Do
        Workbooks.Open StrPath & x
        If Err.Number = 1234 Then
            Application.Wait DateAdd("s", 5, Now)
        Else
            MsgBox "Error " & Err.Number & vbNewLine & Err.Description
        End If
    Loop While Err.Number = 1234
    On Error GoTo 0
Change the 2 instances of '1234' with the error number you get when there is no response from the internet.
 
Upvote 0
Change the 2 instances of '1234' with the error number you get when there is no response from the internet.

Thank you John. However, there is no error number. The page simply won't load much in the way pages don't always load using an internet browser. I can manually cancel the download and try again, but of course I just want to automate this process.
 
Upvote 0
In that case you could use an asynchronous GET request with WinHttpRequest to see if the URL can be opened within a specific time, and if so execute the Workbooks.Open on the URL. WinHttpRequest can also download the file to your computer, from where you can call Workbooks.Open on the local file.

For examples of WinHttpRequest in VBA, see Asynchronous File Downloads from Within VBA (Excel) - Stack Overflow (answered Oct 17 '11 at 3:37 TheFuzzyGiggler) and determine when lost connection on downloads. In particular, pay attention to the http.SetTimeouts method and the http_OnError event, which tells you the error if the connection or GET request fails.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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