将HTML单元格数据拉入Excel工作表

时间:2015-12-21 20:02:40

标签: excel vba htmlelements

我尝试使用VBA从下面的td标记(<td><font class="s1">52.84</font>)中获取单个单元格并将其发布到Excel工作表中。

请参阅下面的HTML剪辑:

<table cellpadding="0" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td bgcolor="#999999">
                <table cellpadding="1" cellspacing="1" border="0" width="100%">
                    <tbody>
                        <tr bgcolor="#cccccc" align="right" height="20">
                            <td><font class="s1">Price</font></td>
                            <td><font class="s1">Change&nbsp;(%)</font><img src="/design/images/0.gif" width="4" height="1" border="0/"></td>
                            <td><font class="s1">52&nbsp;wk&nbsp;High</font><img src="/design/images/0.gif" width="4" height="1" border="0/"></td>
                            <td><font class="s1">52&nbsp;wk&nbsp;Low</font><img src="/design/images/0.gif" width="4" height="1" border="0/"></td>
                            <td><font class="s1">Stock volume </font><a href="javascript:openHelp(14)" alt="Open Help"><img src="/design/images/ico/q_zn.gif" width="8" height="10" border="0" alt="Open Help"></a><img src="/design/images/0.gif" width="4" height="1" border="0/"></td>
                            <td><font class="s1">1WK Avg Opt Volume</font><a href="javascript:openHelp('avg_1wk_ov')" alt="Open Help"><img src="/design/images/ico/q_zn.gif" width="8" height="10" border="0" alt="Open Help"></a><img src="/design/images/0.gif" width="3" height="1" border="0/"></td>
                            <td><font class="s1">1WK Avg Opt OI</font><a href="javascript:openHelp('avg_1wk_oi')" alt="Open Help"><img src="/design/images/ico/q_zn.gif" width="8" height="10" border="0" alt="Open Help"></a><img src="/design/images/0.gif" width="3" height="1" border="0/"></td>
                            <td><font class="s1">EOD Opt Volume</font><a href="javascript:openHelp('eod_ov')" alt="Open Help"><img src="/design/images/ico/q_zn.gif" width="8" height="10" border="0" alt="Open Help"></a><img src="/design/images/0.gif" width="3" height="1" border="0/"></td>
                            <td><font class="s1">EOD Opt OI</font><a href="javascript:openHelp('eod_oi')" alt="Open Help"><img src="/design/images/ico/q_zn.gif" width="8" height="10" border="0" alt="Open Help"></a><img src="/design/images/0.gif" width="3" height="1" border="0/"></td>
                        </tr>
                        <tr bgcolor="#FFFFFF" align="right" height="20"> ***
                            <td><font class="s1">52.84</font>*** </td>
                            <td><font class="s1"><nobr>&nbsp;&nbsp;<img src="/design/images/ico/arrow_bottom.gif" alt="-" border="0" align="absmiddle" width="7" height="9/">&nbsp;-1.06&nbsp;(-1.97%)</nobr></font></td>
                            <td><font class="s1"><nobr>&nbsp;&nbsp;60.34&nbsp;07/22/2015</nobr></font></td>
                            <td><font class="s1"><nobr>&nbsp;&nbsp;46.95&nbsp;01/30/2015</nobr></font></td>
                            <td><font size="-2" class="s1">17,092,380</font></td>
                            <td><font size="-2" class="s1">87,610</font></td>
                            <td><font size="-2" class="s1">1,820,400</font></td>
                            <td><font size="-2" class="s1">54,450</font></td>
                            <td><font size="-2" class="s1">1,858,857</font></td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

由于您没有发布网址,这只是猜测。 。 。

Sub Test() Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")
With IE
    .Visible = True
    .Navigate "http://www.marketwatch.com/investing/stock/aapl/analystestimates" ' should work for any URL
    Do Until .ReadyState = 4: DoEvents: Loop

        x = .document.body.innertext
        y = InStr(1, x, "Average Target Price:")
        Z = Mid(x, y, 6)

        Range("A1").Value = Trim(Z)

        .Quit
    End With
End Sub