Excel Dynamic Web 从表中查询特定数据并使用 VBA 代码转置结果
我正在尝试在 Excel 中编写宏来网络查询多个站点以从表中检索特定数据。 Web 查询正在获取 A 列中的数据并在 C 列中显示结果。问题是该表显示在几行中,而我只需要两行(日期和价格);其余的要删除。结果应转置在 B 列和 C 列中。(每小时刷新一次)。查询如何注意获取所需的数据,并循环运行 A 列中的其他行并显示在 C 和 D 中。由于我是 VBA
A B c D
Site Date/Time Price
74156 xxx yyy
85940
....
....
代码的新手,因此感谢帮助和支持,如下
Sub test1()
Dim qt As QueryTable
Set qt = ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.petro-canada.ca/en/locations/4085.aspx?MODE=DTS&ID=" & Range("A2").Value, Destination:=Range("c2"))
With qt
.Name = "Regular, Posted, Self serve"
.WebSelectionType = xlSpecifiedTables
.WebTables = "20" ' Regular table
.WebFormatting = xlWebFormattingNone
.EnableRefresh = True
.RefreshPeriod = 60 'Unit in minutes
.Refresh 'Execute query
End With
End Sub
I am trying to write macro in excel to web query several sites to retrieve specific data from table. The web query is taking data in column A and displays results in Column C. The thing is that the table is being displayed in several rows and only two I need (date and price); rest to be deleted. The results should be transpose in columns B and C.(refresh every hour). How the query could take care to fetch the required data and also to run in loop for other rows in column A and displays in C and D. Help and support is appreciated since I am new to VBA
A B c D
Site Date/Time Price
74156 xxx yyy
85940
....
....
code is as follows
Sub test1()
Dim qt As QueryTable
Set qt = ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.petro-canada.ca/en/locations/4085.aspx?MODE=DTS&ID=" & Range("A2").Value, Destination:=Range("c2"))
With qt
.Name = "Regular, Posted, Self serve"
.WebSelectionType = xlSpecifiedTables
.WebTables = "20" ' Regular table
.WebFormatting = xlWebFormattingNone
.EnableRefresh = True
.RefreshPeriod = 60 'Unit in minutes
.Refresh 'Execute query
End With
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的网络查询放在不同的页面上,然后在每次刷新时将所需的数据拉入列表中。这是一个例子。
可以在 http://www.dailydoseofexcel.com/excel/PetroWeb.xls
Put your web query on a different page, then pull the data you need into your list on every refresh. Here's an example.
An example can be found at http://www.dailydoseofexcel.com/excel/PetroWeb.xls