Excel VBA – 如何将网页中的文本导入单个单元格

Dim lastRow As Integer lastRow = Range("a1").End(xlDown).Row Dim url As String For i = 2 To lastRow Step 1 strUrl = Range("a" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://politicsandwar.com/api/nation/id=" & strUrl, Destination:=Range("S" & i)) End With Next 

我想从一个特定的网站全文拉到一个单元格,当我运行这个,我的屏幕灰色一两分钟,实际上并没有把任何东西放在目标单元格。 例如,第一行(单元格A2 )将使用来自"7687"数据。

添加.Refresh作为With ... End With块中的最后一个语句,如下所示:

 For i = 2 To lastRow Step 1 strUrl = Range("a" & i).Value With ActiveSheet.QueryTables.Add(Connection:= _ "URL;https://politicsandwar.com/api/nation/id=" & strUrl, Destination:=Range("S" & i)) .Refresh End With Next 

看看这个 。

这应该做你想要的。

 Sub Sample() Dim ie As Object Dim retStr As String Set ie = CreateObject("internetexplorer.application") With ie .Navigate "http://www.wikihow.com/Choose-an-Email-Address" .Visible = True End With Do While ie.readystate <> 4: Wait 5: Loop DoEvents retStr = ie.document.body.innerText '~> Write the above to a text file Dim filesize As Integer Dim FlName As String '~~> Change this to the relevant path 'Save as Text File 'FlName = "C:\Users\Siddharth\Desktop\Sample.Txt" Range("A1").Value = retStr filesize = FreeFile() 'Open FlName For Output As #filesize 'Print #filesize, retStr Close #filesize End Sub Private Sub Wait(ByVal nSec As Long) nSec = nSec + Timer While nSec > Timer DoEvents Wend End Sub 

基于此。

从IE对象中返回整个页面文本