superfurry
New Member
- Joined
- Feb 15, 2010
- Messages
- 4
Hi all,
Struggling with a macro that I am using to scrape some data from a webpage. The code is as follows and is placed in a Module.
Sub getdataEnglish()
Application.Calculate
Dim result As String
Dim myURL As String
Dim winHttpReq As Object
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "http://www.bbc.co.uk/news"
winHttpReq.Open "GET", myURL, False
winHttpReq.Send
result = winHttpReq.responseText
Application.ScreenUpdating = True
Range("Macros!A2").Value = result
Application.OnTime Now + TimeValue("00:02:00"), "getdataEnglish"
End Sub
All works perfectly until I open another spreadsheet. I then get the run time error 1004 when the macro is run. It is failing at the Range("Macros!A2") line. I presume it is because I don't have a sheet in the new workbook that I have opened called macros? Does anyone have any good ideas for a workaround which means that the macro will run even if there is another workbook open?
As an aside, my macro is downloading all of the html on the page. Would it be possible to use the same macro but for it to only extract data from a particular heading? ie. so it doesn't download the whole webpage every time. e.g. I only want to scrape a small part of the website - not everything!
Any help would be much appreciated.
Bert
Struggling with a macro that I am using to scrape some data from a webpage. The code is as follows and is placed in a Module.
Sub getdataEnglish()
Application.Calculate
Dim result As String
Dim myURL As String
Dim winHttpReq As Object
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "http://www.bbc.co.uk/news"
winHttpReq.Open "GET", myURL, False
winHttpReq.Send
result = winHttpReq.responseText
Application.ScreenUpdating = True
Range("Macros!A2").Value = result
Application.OnTime Now + TimeValue("00:02:00"), "getdataEnglish"
End Sub
All works perfectly until I open another spreadsheet. I then get the run time error 1004 when the macro is run. It is failing at the Range("Macros!A2") line. I presume it is because I don't have a sheet in the new workbook that I have opened called macros? Does anyone have any good ideas for a workaround which means that the macro will run even if there is another workbook open?
As an aside, my macro is downloading all of the html on the page. Would it be possible to use the same macro but for it to only extract data from a particular heading? ie. so it doesn't download the whole webpage every time. e.g. I only want to scrape a small part of the website - not everything!
Any help would be much appreciated.
Bert