我刚学习VBA
。我想制作一些从互联网到表格废弃的工具。
我刚刚阅读了一些教程,并在模块中完成了该功能:
Sub URL_Static_Query()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://randompage.com", _
Destination:=Range("j20"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
End Sub
现在我想要的是在写入单元格之前使用提取数据。有任何想法吗?
答案 0 :(得分:0)
您可以尝试抓取网站的HTML,而不是使用查询从网站中提取数据:
Sub ImportData()
'to refer to the running copy of Internet Explorer
Dim IE As InternetExplorer
'to refer to the HTML document returned
Dim html As HTMLDocument
'open Internet Explorer in memory, and go to website
Set IE = New InternetExplorer
IE.Visible = False
IE.Navigate "https://randompage.com"
'Wait until IE is done loading page
Do While IE.ReadyState <> READYSTATE_COMPLETE
Application.StatusBar = "Trying to go to StackOverflow ..."
DoEvents
Loop
'show text of HTML document returned
Set html = IE.Document
MsgBox html.DocumentElement.innerText '--> do your stuff here
'close down IE and reset status bar
Set IE = Nothing
Application.StatusBar = ""
End Sub
要运行上面的代码,您必须添加对以下两个对象库的引用:
1。 Microsoft Internet Controls
2。 Microsoft HTML对象库
您可以在VBE中添加引用。点击Tools
菜单,然后选择References
。然后检查两个库。见下图:
得到here的帮助。