Macro not running...

Dystar

New Member
Joined
Sep 2, 2015
Messages
30
Hi,
I've been following WiseOwl's webscraping instructions and amending them for my own purposes. The script works without bugs (so far...) but when I try to run it out of Debug mode it doesn't seem to work, the Status Bar changes when I try to run it either through the developer menu or a button, but nothing actually happens unless I F8 all the way through the code and then it works perfectly.... Any idea what is going wrong and why it won't run?

Code:
Option Explicit

Enum READYSTATE
READYSTATE_UNINITIALIZED = 0
READYSTATE_LOADING = 1
READYSTATE_LOADED = 2
READYSTATE_INTERACTIVE = 3
READYSTATE_COMPLETE = 4
End Enum

Sub ImportHTMLFromSource(URL As String)
Dim pageCounter As Integer
Dim source As String
For pageCounter = 1 To 1000
source = URL + "allresults/" + CStr(pageCounter)
'to refer to the running copy of Internet Explorer
Dim ie As InternetExplorer
'to refer to the HTML document returne
Dim html As HTMLDocument
'open Internet Explorer in memory, and go to website
Set ie = New InternetExplorer
ie.Visible = False
ie.navigate source
'Wait until IE is done loading page
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Loading URL ..."
DoEvents
Loop
'show text of HTML document returned
Set html = ie.document
'close down IE and reset status bar
Set ie = Nothing
Application.StatusBar = ""

Dim ArticleList As IHTMLElementCollection
Dim Article As IHTMLElement
Dim ArticleURL As String
Dim counter As Integer
counter = SCFindLastRow(1) + 1

Set ArticleList = html.getElementsByClassName("story noThumb")
If ArticleList.Length > 0 Then
    For Each Article In ArticleList
    ArticleURL = Article.innerHTML
    Dim URLLength As Integer
    URLLength = Len(ArticleURL)
    Dim URLStart As Integer
    URLStart = InStr(ArticleURL, "http://www.nytimes.com") - 1
    ArticleURL = Right(ArticleURL, URLLength - URLStart)
    URLLength = Len(ArticleURL)
    Dim URLEnd As Integer
    URLEnd = InStr(ArticleURL, ".html") + 5
    ArticleURL = Left(ArticleURL, URLLength - (URLLength - URLEnd))
    Cells(counter, 1) = ArticleURL
    counter = counter + 1
    Next
Else
    Exit Sub
End If
Next
End Sub

Sub test()
ImportHTMLFromSource ("http://query.nytimes.com/search/sitesearch/#/house+price/from19840101to19840101/")

End Sub
Public Function SCFindLastRow(ByVal ColNum As Integer) As Integer
    SCFindLastRow = Cells(65536, ColNum).End(xlUp).Row
End Function
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
what error do you get?

Thats the weird thing, I don't get an error. I've changed "ie.Visible = False" to True and when I run the macro it loads the IE window and navigates to the page then just stops and returns to the waiting state.
 
Upvote 0
Last edited:
Upvote 0
OK done a bit more digging... it seems the page isn't fully loaded when it exits the Do...While loop so when it checks whether ArticleList.Length > 0 it is going to the Else statement and exiting the sub. Are there any alternatives to this Do...While loop for waiting for the page to load?
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,935
Members
449,195
Latest member
Stevenciu

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