在GetWebTable中为连接使用单元格引用
问题描述:
我试图从WSJ期货中提取表格,但我希望能够更改网址中的日期。我的希望是使用下面的代码,但使网站的单元格参考。任何想法如何使这项工作或一个不同的代码,将使我与网站的单元格参考相同的结果?在GetWebTable中为连接使用单元格引用
Sub GetWebTable()
With ActiveSheet.QueryTables.Add(Connection:="http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170901.html?mod=mdc_pastcalendar", Destination:=Range("a1"))
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub
答
试试这个。它会把你的桌子拿走。仔细看一下脚本中的Connection:="URL;" & URL,
部分。你搞砸了。
Sub GetWebTable()
Dim URL As String
URL = "http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170901.html?mod=mdc_pastcalendar"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub
编辑:
如果你喜欢解析比其他任何特定的表,你只需要找到就像我在下面做了它的索引号:
Sub GetWebTable()
Dim URL As String
URL = "http://www.wsj.com/mdc/public/page/2_3023-fut_metal-futures-20170901.html?mod=mdc_pastcalendar"
With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
.WebTables = "3" 'You can parse any specific table as well. For more than one table change the parameter "2,3" like this
.Refresh
End With
End Sub
这拉表,但因为它也具有编码的网址,它每次都会提取今天的数据,并且不会随着日期的变化而跟随A1地址。 (日期是代码的20170901部分)有关如何避开日历功能的其他想法? –