如何从Excel数据中获取http链接?

时间:2013-10-19 00:07:37

标签: excel excel-vba excel-formula vba

我正在尝试从这些数据中获取所有http链接。

<a class="l-link-w clearfix" data-target-weblink target="_blank" href='http://www.havenswift-hosting.co.uk/clients/link.php?id=17' data-company-item="visitwebsite" data-omniture="WL">
<a class="l-link-w clearfix" data-target-weblink target="_blank" href='https://www.rathosting.com/' data-company-item="visitwebsite" data-omniture="WL">
<a class="l-link-w clearfix" data-target-weblink target="_blank" href='http://www.glxwebhosting.co.uk/' data-company-item="visitwebsite" data-omniture="WL">
<a class="l-link-w clearfix" data-target-weblink target="_blank" href='http://www.immersivemedia.co.uk' data-company-item="visitwebsite" data-omniture="WL">

1 个答案:

答案 0 :(得分:0)

假设上面的来源是A1:

Dim source As String
source = Range("A1").Value

i = InStr(1, source, "http", vbBinaryCompare)
e = InStr(1, source, "data-company", vbBinaryCompare)

link = Mid(source, i, (e - i - 1))
MsgBox (link)

对于多个值,您可以创建一个读取所有行的循环...只需用一些代码替换范围(“A1”)。上面的值。像这样:

      Dim numRows As Integer

      'Replace the A1 with the location where you want your data
      Dim sourceData As Range
      Set sourceData = Range("A1")

      'Replace the A15 to where you want the data
      Dim destData As Range
      Set destData = Range("A15")

      'Set this to the number of rows in the source data
      numRows = 4

      For k = 1 To numRows
        Dim source As String

        'Replace A1 with the cell of the first line of data
        source = sourceData.Offset(k, 0).Value

        i = InStr(1, source, "http", vbBinaryCompare)
        e = InStr(1, source, "data-company", vbBinaryCompare)

        If ((i > 0) And (e > 0)) Then
            link = Mid(source, i, (e - i - 1))
            destData.Offset(k, 0).Value = link
        End If


      Next k