简体   繁体   English

Excel VBA - 将数据从函数分离到 - >单元格

[英]Excel VBA - Separate data from function to-> cell

I'm just learning VBA . 我刚刚学习VBA I want to make some tool that scrap from internet to sheet. 我想制作一些从互联网到表格废弃的工具。

I just read few tutorials, and got that function made in Modules: 我刚刚阅读了一些教程,并在模块中完成了该功能:

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

Now all I want is to use extract data before it writes to cell. 现在我想要的是在写入单元格之前使用提取数据。 Any ideas? 有任何想法吗?

Instead of extracting data from a website using a query, you can try scraping a website's HTML as follows: 您可以尝试抓取网站的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

To run the above code you'll have to add reference to following two object libraries: 要运行上面的代码,您必须添加对以下两个对象库的引用:

1. Microsoft Internet Controls 1. Microsoft Internet Controls
2. Microsoft HTML Object Library 2. Microsoft HTML对象库

You can add reference in VBE. 您可以在VBE中添加引用。 Click on Tools menu and select References . 单击“ Tools菜单,然后选择“ References Then check both the libraries. 然后检查两个库。 See the image below: 见下图:

在此输入图像描述

Got help from here . 这里得到帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM