【问题标题】:Getting contents of link into excel via VBA通过 VBA 将链接内容获取到 excel 中
【发布时间】:2014-04-19 12:30:54
【问题描述】:

我已经问过这个问题几次,但我觉得我没有达到我想要的。这里有几个人提供了帮助,但我仍然有使用数据的问题,因为它的格式不可用。

我希望通过 VBA 将网站上链接的内容放入工作表中

链接位于网页的右上角。

链接是

http://bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet

到目前为止的代码:

 Set ie = CreateObject("InternetExplorer.Application")
        ie.Navigate "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMUTop"
        ie.Visible = True

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    ie.Document.getelementbyid("param5").Value = "2014-04-16"
    ie.Document.getelementbyid("param6").Value = "43"
    ie.Document.getelementbyid("go_button").Click

    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next    ' sometimes more web pages are counted than are open
        my_url = objShell.Windows(x).Document.Location
        my_title = objShell.Windows(x).Document.Title

        If my_url Like "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet" Then
            Set ie = objShell.Windows(x)
            Exit For
        Else
        End If
    Next

For Each ele In ie.Document.getElementsByTagName("span")

    If ele.innerhtml = "Current data in CSV format" Then
        DoEvents
        ele.Click
        'At this point you need to Save the document manually
        ' or figure out for yourself how to automate this interaction.
    End If
Next

 If my_url Like "about:blank" Then
Set ie = objShell.Windows(x)
Else
End If

 table_html = ie.Document.getElementsByTagName(("Text"))(2).innerhtml
    html_lines = Split(table_html, Chr(10), -1, vbTextCompare)

    Worksheets("Sheet1").Activate
    Range("A1").Select

    For x = 0 To UBound(html_lines)
        ActiveCell = html_lines(x)
        ActiveCell.Offset(1, 0).Select
    Next

【问题讨论】:

  • 你为什么不链接到其他问题并解释为什么你觉得你没有实现它?到目前为止你做了什么?
  • 你的问题不是很清楚;这可能就是为什么你没有得到你想要的答案。您希望 VBA 代码究竟做什么?
  • 如果您打开链接,在页面内有一个链接,称为当前数据,格式为 csv。我想通过 VBA 自动将其内容放入电子表格中。
  • 到目前为止,这里有人帮助我将表格从实际网页复制到电子表格中。但我很快意识到这不是下降的路线

标签: excel vba hyperlink


【解决方案1】:

这是你想要的吗?

Sub Test()
    Set ie = CreateObject("InternetExplorer.Application")
        ie.Navigate "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet"
        ie.Visible = True

    Do Until Not ie.Busy And ie.readyState = 4
        DoEvents
    Loop

    ie.ExecWB 17, 2     ' select the data
    ie.ExecWB 12, 0     ' copy the data

    ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False  ' paste the data
End Sub

【讨论】:

  • 好东西。他需要解析表格,但这很好。
  • @ron 你好,这好多了,我只需要自己整理一下就可以了。这正是我所需要的。感谢 Ron 和 David 跟进此事
  • @David Zemens 不确定解析出来是什么意思?那会做什么?
  • @Ingram 例如,如果您需要进行文本到列或其他格式设置以将表格单元格数据放入 Excel 中的有组织的表格中。否则,如果您对这样的直接复制/粘贴感到满意,则无需解析或组织数据。干杯。
猜你喜欢
  • 2012-10-25
  • 2012-11-30
  • 2017-01-16
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多