I have a list of 400 stock symbols in cell A1 of an excel spreadsheet. I then go to a macro and load this website:
https://finviz.com/quote.ashx?t=" & Range("A1").Value
The macro, parsehtml_0 below pulls data into excel from 400 snapshot stock tables. The results load starting in row 1-400 of the spreadsheet.
The problem is that 400 is the limit of snapshot stock tables you can bring in on 1 page and I have many more.
Therefore, I have to make a second macro, parsehtml_1 with 400 more stock symbols in cell A2 to load in 400 more stock symbols by loading this website:
https://finviz.com/quote.ashx?t=" & Range("A2").Value
These results load starting in row 401 to 800.
My question is that since most of the code repeats, is there any way to run a loop to reduce the amount of code and macros. Thanks so much.
Public Sub parsehtml_0() Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, titleElem2 As Object, detailsElem As Object, topic As HTMLHtmlElement Dim i As Integer URL = "https://finviz.com/quote.ashx?t=" & Range("A1").Value Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", URL, False http.send html.body.innerHTML = http.responseText Set topics = html.getElementsByClassName("snapshot-table2") i = 1 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(2) Set titleElem2 = topic.getElementsByTagName("td")(1) Sheets(1).Cells(i, 3).Value = titleElem.getElementsByTagName("b")(0).innerText Set titleElem = topic.getElementsByTagName("tr")(3) Set titleElem2 = topic.getElementsByTagName("td")(2) Sheets(1).Cells(i, 4).Value = titleElem.getElementsByTagName("b")(0).innerText i = i + 1 Next Set topics = html.getElementsByClassName("fullview-title") i = 1 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(0) Set titleElem2 = topic.getElementsByTagName("td")(0) Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).innerText i = i + 1 Next End Sub Public Sub parsehtml_1() Dim http As Object, html As New HTMLDocument, topics As Object, titleElem As Object, titleElem2 As Object, detailsElem As Object, topic As HTMLHtmlElement Dim i As Integer URL = "https://finviz.com/quote.ashx?t=" & Range("A2").Value Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", URL, False http.send html.body.innerHTML = http.responseText Set topics = html.getElementsByClassName("snapshot-table2") i = 401 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(2) Set titleElem2 = topic.getElementsByTagName("td")(1) Sheets(1).Cells(i, 3).Value = titleElem.getElementsByTagName("b")(0).innerText Set titleElem = topic.getElementsByTagName("tr")(3) Set titleElem2 = topic.getElementsByTagName("td")(2) Sheets(1).Cells(i, 4).Value = titleElem.getElementsByTagName("b")(0).innerText i = i + 1 Next Set topics = html.getElementsByClassName("fullview-title") i = 401 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(0) Set titleElem2 = topic.getElementsByTagName("td")(0) Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).innerText i = i + 1 Next End Sub When I add that code, see below, the macro parsehtml dissapears. If I run the code anyway or run loader, I get an error on this line with the bolded part highlighted.
Dim http As Object**, html As New HTMLDocument**, topics As Object, titleElem As Object, titleElem2 As Object, detailsElem As Object, topic As HTMLHtmlElement
Public Sub parsehtml(page As String) Dim http As Object**, html As New HTMLDocument**, topics As Object, titleElem As Object, titleElem2 As Object, detailsElem As Object, topic As HTMLHtmlElement Dim i As Integer URL = "https://finviz.com/quote.ashx?t=" & page Set http = CreateObject("MSXML2.XMLHTTP") http.Open "GET", URL, False http.send html.body.innerHTML = http.responseText Set topics = html.getElementsByClassName("snapshot-table2") i = 1 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(2) Set titleElem2 = topic.getElementsByTagName("td")(1) Sheets(1).Cells(i, 3).Value = titleElem.getElementsByTagName("b")(0).innerText Set titleElem = topic.getElementsByTagName("tr")(3) Set titleElem2 = topic.getElementsByTagName("td")(2) Sheets(1).Cells(i, 4).Value = titleElem.getElementsByTagName("b")(0).innerText i = i + 1 Next Set topics = html.getElementsByClassName("fullview-title") i = 1 For Each topic In topics Set titleElem = topic.getElementsByTagName("tr")(0) Set titleElem2 = topic.getElementsByTagName("td")(0) Sheets(1).Cells(i, 2).Value = titleElem.getElementsByTagName("a")(0).innerText i = i + 1 Next End Sub Sub Loader() parsehtml Range("A1").Value parsehtml Range("A2").Value End Sub 