当我使用"从网上获取数据"通过excel命令,数据完美地粘贴到Excel中。但我想使用vba自动执行此操作。
以下是我正在使用的示例HTML:
<html><head>
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<meta name="robots" content="index,follow">
<title>"title: abcdefg"</title>
</head>
<h1>"heading: 2345"</h1>
<p>
Last Updated: "Last date"
</p><p>
</p><p>
<next></next><table border="3" cellpadding="6" cellspacing="3">
<tbody><tr>
<td>
<pre>
*Want this data*
</pre>
</td>
</tr>
<tr>
<td>
<pre>
*Want this data*
</pre>
</td>
</tr>
<tr>
<td>
<pre>
*Want this data*
&#13;
到目前为止,这是我的VBA代码:
Private Sub Test()
Dim ie As Object, i As Long, strText As String
Dim doc As Object, hTable As Object, hBody As Object, hTR As Object, hTD As Object
Dim tb As Object, bb As Object, tr As Object, td As Object
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
Set wb = Excel.ActiveWorkbook
Set ws = wb.ActiveSheet
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
y = 1 'Column A in Excel
z = 1 'Row 1 in Excel
ie.navigate "my URL HERE", , , , "Content-Type: application/x-www-form-urlencoded" & vbCrLf
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
Set hTable = doc.getElementsByTagName("table")
For Each tb In hTable
Set hBody = tb.getElementsByTagName("tbody")
For Each bb In hBody
Set hTR = bb.getElementsByTagName("tr")
For Each tr In hTR
Set hTD = tr.getElementsByTagName("td")
y = 1 ' Resets back to column A
For Each td In hTD
ws.Cells(z, y).Value = td.innerText
y = y + 1
Next td
DoEvents
z = z + 1
Next tr
Exit For
Next bb
Exit For
Next tb
'MsgBox ("it worked")
End Sub
代码一直运行而没有任何错误,但没有任何内容发布到Excel工作表上。请帮忙!我不熟悉HTML / VBA。