2016-05-31 48 views
1

我刚刚学习VBA。我想制作一些从互联网到报表的工具。Excel 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

你可能会发现从Wise Owl这篇文章很有用[如何使用VBA从网页上刮取数据或HTML表格](http://www.wiseowl.co.uk/blog/s393/scrape-website-html.htm )。它一步一步概述如何获取Stackoverflow的主要页面数据。 – skkakkar

+0

哟,skkakkar - 不幸的是页面只有​​。 –

回答

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控制
2. Microsoft HTML对象库

您可以在VBE中添加引用。点击Tools菜单并选择References。然后检查两个库。见下图:从here

enter image description here

得到了帮助。

+0

@ R.Michael - 这有助于解决您的问题? – Mrig