使用VBA从HTML表格复制/粘贴,粘贴到Excel

使用VBA从HTML表格复制/粘贴,粘贴到Excel,vba,excel,Vba,Excel,我正在Excel中使用VBA。我希望使用VBA仅复制HTML表中的某些数据。我正在使用的表如下所示: <table class="RatingsTable standard" id="RatingsTable1"> <tr> <th class="top_header" colspan="16">General & Fielding Ratings</th>

我正在Excel中使用VBA。我希望使用VBA仅复制HTML表中的某些数据。我正在使用的表如下所示:

<table class="RatingsTable standard" id="RatingsTable1">
                <tr>
                    <th class="top_header" colspan="16">General & Fielding Ratings</th>
                </tr>
                <tr>
                    <th class="event">Event</th><th class="season">Season</th><th class="height">Height</th><th class="weight">Weight</th><th class="rating overall" title="Overall"><span class="hidden">OV</span></th><th class="rating range" title="Range"><span class="hidden">RA</span></th><th class="rating glove" title="Glove"><span class="hidden">GL</span></th><th class="rating armstrength" title="Arm Strength"><span class="hidden">AS</span></th><th class="rating armaccuracy" title="Arm Accuracy"><span class="hidden">AA</span></th><th class="rating pitchcalling" title="Pitch Calling"><span class="hidden">PC</span></th><th class="rating durability" title="Durability"><span class="hidden">DU</span></th><th class="rating health" title="Health"><span class="hidden">HE</span></th><th class="rating speed" title="Speed"><span class="hidden">SP</span></th><th class="rating patience" title="Patience"><span class="hidden">PA</span></th><th class="rating temper" title="Temper"><span class="hidden">TP</span></th><th class="rating makeup" title="Makeup"><span class="hidden">MK</span></th>
                </tr>

                <tr class="odd">
                    <td class="event">Current</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>
                </tr>

                <tr class="even">
                    <td class="event">Projected</td><td class="season">-</td><td class="height">?</td><td class="weight">?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td><td>?</td>
                </tr>

                <tr class="odd">
                    <td class="event">Spring Training</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>
                </tr>

            </table>

通用和现场评级
事件季节性高权重OVRAGLASAAPCDUHESSPPATPMK
当前366-022487291085462225936553680
预计-??????????????
弹簧系366-0224872910854622259316553680
我希望复制和粘贴的数据如下:

<td class="event">Current</td><td class="season">36</td><td class="height">6-0</td><td class="weight">224</td><td>87</td><td>29</td><td>10</td><td>85</td><td>46</td><td>22</td><td>25</td><td>93</td><td>16</td><td>55</td><td>36</td><td>80</td>
Current366-0224872910854622259316553680
所以,我需要从这个特定玩家的表中复制36,6-0,224,87,29,10,85,46,22,25,93,16,55,36和80,但我无法获取这个特定的数据。有人能提供帮助吗?

在Excel菜单(2007/2010或更高版本)中选择“
数据”
”选项卡,然后选择“
来自Web
”,然后输入URL并使用该箭头图标突出显示感兴趣的HTML文档表,然后在Excel工作表中指定目标单元格

您可以使用宏记录器生成模板VBA
Sub
,然后根据特定目的对其进行微调。这个过程在微软的文章中有很好的文档记录:(它还描述了在Excel中创建Web查询的方法,您可以使用它)

根据注释添加:为了减小与业务逻辑相关的表的大小,如果外部网站提供此选项,则可以创建参数化的自定义web查询。最通用的解决方案是使用最适合您的目标的Web数据填充Excel工作表,然后(根据需要)使用Excel VBA执行最终数据修剪

仅供参考:还有一种下载/解析整个HTML文件的技术,但我不推荐这种方法


希望这能有所帮助。向您致意,

我可以给您一个更精确的方法。您感兴趣的是能够从表的各个部分中进行选择

您可以看到,您所追求的是表中带有
id=“RatingsTable1”
的最后一个
tr
。表的最后一行

我们可以使用CSS选择器来访问描述定位的内容

#RatingsTable1 tr:last-child
上面说的是id为RatingsTable1的元素内部元素的最后一个子元素
tr
标记

同样有
第一个子项
第n个子项
选择器


CSS查询:


VBA:

您可以通过
文档

你没有显示任何代码,但说你正在使用ie,那么它将是

ie.document.querySelector("#RatingsTable1 tr:last-child").innerText
如果您有一个html文档变量,例如htmlDoc,那么它将是:

htmlDoc.querySelector("#RatingsTable1 tr:last-child").innerText

这个问题发布已经有一段时间了,但由于我最近一直在从事类似的项目,我想我可以贡献我的解决方案。下面的方法演示了如何使用VBA解析HTML表的一般逻辑,并且可以进行修改以满足任何类似项目的需要。要使以下函数正常工作,您需要对MS HTML对象库的引用

Public Function parseTableHTML(stringHTML As String, tableID As String, rowClass As String)
    Dim sampleHTML As New MSHTML.HTMLDocument 'create an HTMLDocument object
    Dim tableHTML As HTMLTable
    Dim rowHTML As HTMLTableRow
    Dim cellHTML As HTMLTableCell
    sampleHTML.body.innerHTML = stringHTML 'set the HTMLDocument's body equal to the html code you want to parse
    Set tableHTML = sampleHTML.getElementById(tableID) 'get the element whose ID is equal to tableID (in this case the element you're interested in, is a table with tableID="RatingsTable1")
    Set rowHTML = tableHTML.getElementsByClassName(rowClass)(0) 'get the first row from the collection of rows that belong to the table of interest and their class name is rowClass (in this case rowClass="odd")
    For Each cellHTML In rowHTML.Cells 'loop through the cells that belong to the row of interest
        Debug.Print cellHTML.innerText
    Next cellHTML
End Function
根据相同的逻辑,如果感兴趣的表没有ID,但您知道html代码段中有几个表,并且您对第一个表感兴趣,那么您可以从表集合中获得它:

Set tableHTML = sampleHTML.getElementsByTagName("table")(0)
相同的原则适用于感兴趣的行,在本例中,您可以从行集合中获得如下内容:

Set rowHTML = tableHTML.getElementsByTagName("tr")(2)

这就是我的位置,因为它粘贴了整个表,我希望得到一个更直接的宏。使用此方法,我只能粘贴整个表,然后从我从web粘贴的内容复制和粘贴。我不知道如何从表格中选择答案。我已经根据您的补充意见扩展了答案。如果满意,请将其标记为已接受。如果您有更多问题,请单独发布。顺致敬意,