需要循环帮助

时间:2018-06-14 00:11:29

标签: excel vba

我在excel电子表格的单元格A1中有400个股票代码的列表。然后我去宏观并加载这个网站:

https://finviz.com/quote.ashx?t=" &安培;范围(" A1")。值

下面的宏parsehtml_0将数据从400个快照库存表中拉入excel。结果从电子表格的第1-400行开始加载。

问题是400是你可以在1页上带来的快照库存表的限制,我还有更多。

因此,我必须通过加载此网站制作第二个宏,parsehtml_1,在单元格A2中包含400多个股票代码,以加载400多个股票代码:

https://finviz.com/quote.ashx?t=" &安培;范围(" A2")。值

这些结果从第401行开始加载到800.

我的问题是,由于大多数代码重复,有没有办法运行循环来减少代码和宏的数量。非常感谢。

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

当我添加该代码时,请参见下文,宏parsehtml消失。如果我仍然运行代码或运行加载程序,我会在此行中收到错误,并突出显示粗体部分。

Dim http As Object **,html As New HTMLDocument **,subject 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

1 个答案:

答案 0 :(得分:2)

在子程序中使用参数。

请参阅Microsoft documentation和此additional resource

而不是打电话

Public Sub parsehtml_0()

你应该致电

Public Sub parsehtml(page as String)

然后你可以在Sub中改变一行:

URL = "https://finviz.com/quote.ashx?t=" & Range("A1").Value

变为:

URL = "https://finviz.com/quote.ashx?t=" & page

从那里,您可以创建一个初始Sub,根据需要多次处理循环:

Sub Loader() 
  parsehtml Range("A1").Value
  parsehtml Range("A2").Value
End Sub 

只有两个条目,这样做;如果你继续加载大量页面,你可以研究如何用生成的数字替换A1并将负载包裹在一个循环中。

为了让Excel找到HTMLDocument对象,需要引用。添加参考VBE>工具>参考文献> HTML Object Libary(根据QHarr的评论)。

MS Excel Reference to HTML ObjectLibrary

您的主体代码当前重用了相同的空间。您可以将代码移动到工作簿模块,并将每个页面的输出分配给差异工作表,但最简单的方法是将计数器变量i设为static variable

替换:

Dim i As Integer

使用:

Static i As Integer

这将在运行中保留i的值。请注意,关闭工作簿时将丢失该值。如果要保留该边界的值,只需将计数器分配给单元格 - Range("B1").Value = i