Run-time error trapping question

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Back again folks with another question that is plaguing me and I am hoping someone will rescue me once again.

The site that I am trying to scrape from is https://www.poloniex.com/exchange#btc_dgb

The code that I am using is the following:

Code:
'
        Dim IE As New InternetExplorer
        Dim element As Object
'
'       Allow the Browser window, that we will be scraping values from, to be visible
        IE.Visible = True
'
'    Update status bar to inform the user of what is occurring
        Application.StatusBar = "Loading website … https://www.poloniex.com/exchange#btc_dgb"        
'
'       Browser address that we will be scraping values from
        IE.navigate "https://www.poloniex.com/exchange#btc_dgb"
'
'    Allow mouse clicks and such while info is being scraped from internet ... 
        Do While (IE.Busy Or IE.readyState <> READYSTATE_COMPLETE)            ' Create a delay to allow the webpage to fully load before proceeding
            Application.Wait (Now + TimeValue("00:00:01"))                ' Delay for 1 additional second and slow down the clock cycles consumed by the original loop
            DoEvents                                    ' Original delay/loop to allegedly allow the page to fully load
        Loop
'
        Dim Doc As HTMLDocument
        Set Doc = IE.document
'
'     *** ADDITIONAL DELAY that is sometimes required *** because of occasional 'Run-time error '91': Object variable or With block variable not set'
'    Wait some more because sometimes all of the webpage data is not fully loaded by now
    Application.Wait (Now + TimeValue("00:00:06"))
'
'    Update status bar to inform the user of what is occurring
        Application.StatusBar = "Gathering Data from website … https://www.poloniex.com/exchange#btc_dgb"    
'
'       Return SellPrice
        Range("poloniex_SellPrice_1").Value = Doc.getElementsByTagName("td")(3).innerText
'
'       Return BuyPrice
        Range("poloniex_BuyPrice_1").Value = Doc.getElementsByTagName("td")(2).innerText

What I am wanting to do is create some type of runtime error trapping to speed up the Delays. I currently have an additional delay in case the original 'do while readystate' loop fails to load completely, but sometimes, more or less than 6 additional seconds is required to allow the page to fully load so I can start scraping values.

Is there a way that I can 'trap' for run-time error 91, wait for an additional second, and then go back to see if 'run-time error 91' is still occurring, and if so, wait for an additional second, and go check again? In other words, I want to get rid of the additional 6 second delay and approach it one second at a time until the page is recognized as fully loaded and the run-time error 91 is gone.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Thank you mole999 for your response. I checked out you link and also goggled some more and came up with the following:

Code:
'
        Dim IE As New InternetExplorer
        Dim element As Object
'
'       Allow the Browser window, that we will be scraping values from, to be visible
  IE.Visible = True
'
'    Update status bar to inform the user of what is occurring
        Application.StatusBar = "Gathering Data from website … https://www.poloniex.com/exchange#btc_dgb"    
'
[COLOR=#ff0000]'      *** ADDITIONAL DELAY that is sometimes required *** because of  occasional 'Run-time error '91': Object variable or With block variable  not set'
'    Wait some more because sometimes all of the webpage data is not fully loaded by now
'
Poloniex_Additional_Delay:
    Application.Wait (Now + TimeValue("00:00:01"))
'
'   If an error is encountered, resume the code at next line ie. ignore the error and continue
    On Error Resume Next
'
'       Return SellPrice
        Range("poloniex_SellPrice_1").Value = Doc.getElementsByTagName("td")(3).innerText
'
'   Test to see if we encountered a run-time error 91
    If Err.Number = 91 Then             ' Yes we encountered error 91
        GoTo Poloniex_Additional_Delay  '   So go back and delay for 1 more second
    End If
'
'   No error 91 so Turn off Error handling
    On Error GoTo 0
'[/COLOR]
'   Return BuyPrice
    Poloniex_Buy_Commission_Fee = Doc.getElementById("buyOrderBookTable").getElementsByTagName("tr")(1).Cells(0).innerText * Doc.getElementById("buyOrderBookTable").getElementsByTagName("tr")(1).Cells(1).innerText * Poloniex_Commission_Percent
'
    Range(PoloniexBuyCell).Value = Doc.getElementById("buyOrderBookTable").getElementsByTagName("tr")(1).Cells(0).innerText - Poloniex_Buy_Commission_Fee
'
'   Update the Status Bar
    Application.StatusBar = "Update " & CurrentCoin & " Poloniex Values Complete :)"
'
    IE.Quit

Let me know what everyone thinks folks. That code change seems to work much quicker on my end thus far. I am curious if this is the best solution however.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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