Vba打开一个网址,怎么将查询结果导入excel中的代码
时间: 2024-06-04 14:13:10 浏览: 12
以下是将查询结果导入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
希望对你有帮助!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)