Vba打开一个网址,怎么将查询结果导入excel中的代码
时间: 2024-06-04 21:13:10 浏览: 142
以下是将查询结果导入Excel中的VBA代码:
Sub 导入数据()
Dim IE As Object
Dim doc As Object
Dim targetURL As String
Dim searchValue As String
Set IE = CreateObject("InternetExplorer.Application")
'将以下的网址替换为你需要查询的网址
targetURL = "http://www.samplewebsite.com"
'将以下的查询值替换为你需要查询的值
searchValue = "查询值"
IE.Visible = True
IE.navigate targetURL
'等待网页加载完成,不然会出现错误
Do Until IE.readyState = 4
DoEvents
Loop
Set doc = IE.document
'在网页上输入查询值,并点击查询按钮
doc.getElementById("searchBox").Value = searchValue
doc.getElementById("searchButton").Click
'等待网页加载完成,不然会出现错误
Do Until IE.readyState = 4
DoEvents
Loop
'获取查询结果的数据集合
Set dataCollection = doc.getElementsByClassName("resultData")
'将结果导入Excel中
For Each data In dataCollection
ActiveSheet.Cells(nextRow, 1).Value = data.getElementsByTagName("td")(0).innerText
ActiveSheet.Cells(nextRow, 2).Value = data.getElementsByTagName("td")(1).innerText
nextRow = nextRow + 1
Next data
'关闭Internet Explorer浏览器
IE.Quit
End Sub
希望对你有帮助!
阅读全文