使用Excel VBA将鼠标悬停在网页表格上,并从网页源代码复制表格的步骤如下:
Sub CopyTableFromWebPage()
Dim IE As Object
Dim HTMLDoc As Object
Dim Table As Object
Dim TableBody As Object
Dim TableRow As Object
Dim TableCell As Object
Dim RowIndex As Integer
Dim ColumnIndex As Integer
' 创建Internet Explorer对象
Set IE = CreateObject("InternetExplorer.Application")
' 设置IE对象属性
With IE
.Visible = True ' 可见性设置为True,方便调试
.navigate "https://example.com" ' 替换为目标网页的URL
' 等待IE加载完毕
Do While .Busy Or .readyState <> 4
DoEvents
Loop
End With
' 获取网页源代码
Set HTMLDoc = IE.document
' 根据表格的HTML标签和属性定位表格
Set Table = HTMLDoc.getElementsByTagName("table")(0) ' 替换为目标表格的HTML标签和属性
' 遍历表格的行和列,并将数据复制到Excel中
RowIndex = 1
For Each TableRow In Table.Rows
ColumnIndex = 1
For Each TableCell In TableRow.Cells
' 将单元格的值复制到Excel中
Cells(RowIndex, ColumnIndex).Value = TableCell.innerText
ColumnIndex = ColumnIndex + 1
Next TableCell
RowIndex = RowIndex + 1
Next TableRow
' 清理对象
Set TableCell = Nothing
Set TableRow = Nothing
Set TableBody = Nothing
Set Table = Nothing
Set HTMLDoc = Nothing
Set IE = Nothing
End Sub
注意事项:
推荐的腾讯云相关产品:无
领取专属 10元无门槛券
手把手带您无忧上云