簡體   English   中英

Excel VBA - 將數據從函數分離到 - >單元格

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

我剛剛學習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

現在我想要的是在寫入單元格之前使用提取數據。 有任何想法嗎?

您可以嘗試抓取網站的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 然后檢查兩個庫。 見下圖:

在此輸入圖像描述

這里得到幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM