简体   繁体   English

VBA QueryTables循环类参考

[英]VBA QueryTables loops class reference

What I am trying to do is grab data form mutable URLs, at the same time using cells A1 to A10 as the last string of text on the URLs QueryTables. 我想做的是获取可变形式URL的数据,同时使用单元格A1到A10作为URL QueryTables上的最后一个文本字符串。

  • Example
    Cell A1= B0006SH4PA 单元格A1 = B0006SH4PA
  • URL or QueryTables would changed in reference to cells text URL或QueryTables将更改为引用单元格文本

    myURlocation=B0006SH4PA ' then data scraped from this URL then loop to next cell down myURlocation = B0006SH4PA',然后从该URL抓取数据,然后循环到下一个单元格

  • This would continue to cell A10 with each cell having different test making a different QueryTable for every cell. 这将继续到单元格A10,每个单元格具有不同的测试,从而为每个单元格创建不同的QueryTable。

This is code I have at the moment 这是我目前拥有的代码

Sub URL_Static_Query()
Dim i As Integer

   With ActiveSheet.QueryTables.Add(Connection:= _
      "URL;myURlocation=" & Range("a1"), _
         Destination:=Range("a1"))

      .BackgroundQuery = True
      .TablesOnlyFromHTML = True
      .Refresh BackgroundQuery:=False
      .SaveData = True
   End With
End Sub

Try this, it will write the data starting from column B below each other. 尝试此操作,它将从B列开始依次写数据。

  Dim i As Long
  For i = 1 To 10
     With Sheet1.QueryTables.Add(Connection:="URL;myURlocation=" & Range("a" & i), _
        Destination:=sheet1.Range("b999999").End(xlUp).Offset(1))

        .BackgroundQuery = True
        .TablesOnlyFromHTML = True
        .Refresh BackgroundQuery:=False
        .SaveData = True
     End With
  Next i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM