Excel VBA – 从函数到单元格分离数据

我刚刚学习VBA 。 我想做一些工具,从互联网废料到工作表。

我只读了几个教程,并获得了模块中的function:

 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 

现在我只想在提取数据写入单元格之前使用提取数据。 有任何想法吗?

而不是使用查询从网站中提取数据,您可以尝试抓取网站的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.微软互联网控制
2.微软HTML对象库

你可以在VBE中添加引用。 点击Tools菜单并selectReferences 。 然后检查两个库。 看到下面的图片:

在这里输入图像说明

从这里得到帮助。