0

I'm trying to data scrape the main fundamentals table in finviz using

Public Sub TestRequest()
Dim Html As HTMLDocument, htable As HTMLTable
Set Html = New HTMLDocument
  
With CreateObject("MSXML2.XMLHTTP")
    .Open "GET", "https://finviz.com/quote.ashx?t=AAPL", False
    .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    .send
    Html.body.innerHTML = .responseText
End With

Sheets("Temp").Cells.ClearContents
Sheets("Temp").Cells(1, 1) = Html.getElementsByClassName("snapshot-table2")(0).innerText

However, the entire table data gets dumped into a single cell in Excel. How do I turn the data into a neat table?

1 Answer 1

1

The following code first transfers the data to your worksheet, and then creates a table...

Public Sub TestRequest()

    Sheets("Temp").Cells.ClearContents
    
    Dim Html As HTMLDocument
    Set Html = New HTMLDocument
      
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://finviz.com/quote.ashx?t=AAPL", False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .send
        Html.body.innerHTML = .responseText
    End With
    
    Dim htable As HTMLTable
    Set htable = Html.getElementsByClassName("snapshot-table2")(0)
    
    Dim r As Long
    Dim c As Long
    
    With htable
        For r = 0 To .Rows.Length - 1
            For c = 0 To .Rows(r).Cells.Length - 1
                Sheets("Temp").Cells(r + 1, c + 1).Value = .Rows(r).Cells(c).innerText
            Next c
        Next r
    End With
    
    Sheets("Temp").ListObjects.Add _
        SourceType:=xlSrcRange, _
        Source:=Sheets("Temp").Range("A1").CurrentRegion, _
        xllistobjecthasheaders:=xlNo
    
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.