Run-time Error '1004'

mikeypsooner

New Member
Joined
May 16, 2017
Messages
33
I have the following code below but for some reason I am getting the Run-time Error '1004'. Can anyone please help. I am not sure what this is related. Some information I found states its related to internet connection but I am connected to the internet.

Code:
Sub test()


    Sheet2.Select
    Sheet2.Cells.Clear
    
    qurl = "https://finance.yahoo.com/quote/GOOGL?p=GOOGL"


    With Sheet2.QueryTables.Add(Connection:="URL;" & qurl, Destination:=Sheet2.Range("A1"))
 
        .BackgroundQuery = True
        .Refresh BackgroundQuery:=False


    End With


End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
When I tried it, I too got an error. It looks like you will have to use another method. Which data do you want to retrieve from that web page?
 
Upvote 0
The error is due to the quantity and complexity of the data on the page.
You will need another method to specify which part of the data you need.
 
Upvote 0
I guess just to pull the first table off is okay with me. What would that look like and would it work? Then I can edit how needed.
 
Upvote 0
This method uses Internet Explorer and the HTML object. First, set the following references (VBE > Tool > References)...

Code:
1) Microsoft Internet Controls

2) Microsoft HTML Object Library

Then try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] GetStockData()

    [COLOR=darkblue]Dim[/COLOR] IE [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] SHDocVw.InternetExplorer
    [COLOR=darkblue]Dim[/COLOR] HTMLDoc [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] MSHTML.HTMLDocument
    [COLOR=darkblue]Dim[/COLOR] HTMLRows [COLOR=darkblue]As[/COLOR] MSHTML.IHTMLElementCollection
    [COLOR=darkblue]Dim[/COLOR] HTMLRow [COLOR=darkblue]As[/COLOR] MSHTML.IHTMLElement
    [COLOR=darkblue]Dim[/COLOR] wksDest [COLOR=darkblue]As[/COLOR] Worksheet
    [COLOR=darkblue]Dim[/COLOR] r [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] wksDest = Sheet2
    wksDest.Cells.Clear
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    
    IE.navigate "https://finance.yahoo.com/quote/GOOGL?p=GOOGL"
    IE.Visible = [COLOR=darkblue]False[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] IE
        [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] .Busy [COLOR=darkblue]Or[/COLOR] .readyState <> READYSTATE_COMPLETE
            DoEvents
        [COLOR=darkblue]Loop[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] HTMLDoc = IE.document
    
    [COLOR=darkblue]With[/COLOR] HTMLDoc.getElementsByClassName("D(ib) Fw(200) Mend(20px)")(0)
        wksDest.Range("A1").Value = .Children(0).innerText
        wksDest.Range("B1").Value = .Children(1).innerText
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] HTMLRows = HTMLDoc.getElementsByClassName("D(ib) W(1/2) Bxz(bb)")(0).getElementsByTagName("tr")
    
    r = 3
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] HTMLRow [COLOR=darkblue]In[/COLOR] HTMLRows
        wksDest.Cells(r, "A").Value = HTMLRow.Cells(0).innerText
        wksDest.Cells(r, "B").Value = HTMLRow.Cells(1).innerText
        r = r + 1
    [COLOR=darkblue]Next[/COLOR] HTMLRow
    
    [COLOR=darkblue]Set[/COLOR] HTMLRows = HTMLDoc.getElementsByClassName("D(ib) W(1/2) Bxz(bb)")(1).getElementsByTagName("tr")
    
    r = 3
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] HTMLRow [COLOR=darkblue]In[/COLOR] HTMLRows
        wksDest.Cells(r, "D").Value = HTMLRow.Cells(0).innerText
        wksDest.Cells(r, "E").Value = HTMLRow.Cells(1).innerText
        r = r + 1
    [COLOR=darkblue]Next[/COLOR] HTMLRow
    
    Sheet2.Activate
    
    Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    
    MsgBox "Completed...", vbInformation
    
    [COLOR=darkblue]Set[/COLOR] IE = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] HTMLDoc = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] HTMLRows = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] HTMLRow = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] wksDest = [COLOR=darkblue]Nothing[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Here's an example of the results you'll get...

A1:E10

964.61+5.39 (+0.56%)
Previous Close959.22Market Cap658.15B
Open963.55Beta0.92
Bid964.26 x 200PE Ratio (TTM)32.6
Ask964.41 x 100EPS (TTM)29.59
Day's Range960.35 - 965.90Earnings Date‎Jul‎ ‎26‎, ‎2017 - ‎Jul‎ ‎31‎, ‎2017
52 Week Range672.66 - 965.90Dividend & YieldN/A (N/A)
Volume966,086Ex-Dividend DateN/A
Avg. Volume1,495,8631y Target Est1,054.17

<tbody>
</tbody>


Hope this helps!
 
Upvote 0
Domenic this is great and very useful for me.

Building off the work you compiled how would one pull the data from the tables on the webpage https://finance.yahoo.com/quote/VTI/holdings?p=VTI

Looking to get the Overall Portfolio Composition (%), Sector Weightings (%), Equity Holdings, Bond Ratings, Top 10 Holdings (14.91% of Total Assets), and Fund Overview in tables similar to the one you provided. I am not sure how you located table names or the getElementsByClassName variables from the page. Not to familiar with that.
 
Upvote 0
For your new question, I suggest you start a new thread...
 
Upvote 0

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,331
Latest member
smckenzie2016

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