【问题标题】:VBA finding the last instance of a phrase before a different key phrase?VBA在不同的关键短语之前找到短语的最后一个实例?
【发布时间】:2012-07-10 15:05:50
【问题描述】:

我有一个来自 HTML 源代码的大字符串(大约 1,000,000 个字符长)。我正在使用 msinet.ocx 从适当的网站查看文本。我编写了一小段代码,以查找出现在不同关键短语(“组件附件矩阵”)之前的关键短语(“pkid =”),但它无法正常工作。这是我现在拥有的:

workbench = Cells(columnNumber, 1).Value
myURL = "http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document&pkid=" _
& workbench
Dim inet1 As Inet
Dim mypage As String

Set inet1 = New Inet
With inet1
    .Protocol = icHTTP
    .URL = myURL
    mypage = .OpenURL(.URL, icString)
End With

CAMnum = InStr(mypage, "Component Accessory Matrix")
intStart = InStrRev(mypage, "pkid=", CAMnum) + 5
newnum = Mid(mypage, intStart, 6)
Cells(columnNumber, 2).Value = newnum

问题似乎出在mypage = .OpenURL(.URL, icString);当我运行len(mypage) 时,它返回大约 100,000 的值,而它应该返回大约一百万的值。有人可以解释一下吗?

【问题讨论】:

  • 你能告诉我们你尝试了什么吗?

标签: html vba search excel


【解决方案1】:

使用InStrInStrRev,找到你的字符串,然后向后看。一旦你有了那个位置,从那里向前看,直到我们找到引号字符。最后,使用这些位置来获取字符串

pos1 = InStrRev(YourXMLString, "pkid=", InStr(YourXMLString, "Component Accessory Matrix")) + 5 ' length of "pkid="
pos2 = InStr(pos1, YourXMLString, Chr(34)) ' Chr(34)="
FoundString = Mid$(YourXMLString, pos1, pos2 - pos1)

注意如果"Component Accessory Matrix" 缺失,或者前面没有"pkid=",或者引号不是分隔符,或者更多文本位于 pkid= 和引号之间,代码将失败和/或产生无意义的结果。

【讨论】:

    【解决方案2】:

    使用 HTML DOM。

    • 将文本加载到 HTML 文档中:

      Dim html As Object ' MSHTML.HTMLDocument  
      Set html = CreateObject("htmlfile")  
      html.body.innerHTML = "your HTML code here"
      
    • 使用getElementsByTagName 抓取td 元素的集合:

      Dim tdElements As Object ' MSHTML.IHTMLElementCollection  
      Set tdElements = html.getElementsByTagName("td")  
      
    • 遍历它们,直到找到内部文本为“组件附件矩阵”的那个,然后获取前一个 td 元素的内部文本。

      Dim tdElement As Object ' MSHTML.IHTMLElement  
      Dim i As Long  
      Dim textToParse As String  
      For i = 1 to tdElements.Count  
        If tdElements.Item(i).innerText = "Component Accessory Matrix" Then  
          ' get previous <td>  
          Set tdElement = tdElements.Item(i - 1)  
          textToParse = tdElement.innerText  
          Exit For  
        End If  
      Next i  
      
    • 使用传统方法解析字符串

    我会用= 拆分结果字符串并获取第二个元素,然后将该字符串拆分为" 并获取第一个元素。剩下的就是300451

    Here's a further example if you need it.

    【讨论】:

    • 感谢您的回复!但是,我确实遇到了您的解决方案的问题:我拥有的代码很长,并且要从网站下载。另外,我要完成其中的 147 个,所以我必须编写一个宏来从特定网站下载 HTML。到目前为止,这是我所拥有的:
    • workbench = Cells(columnNumber, 1).Value myURL = "http://beams.us.yazaki.com/Beams/ViewDetails.aspx?topic=document&amp;pkid=" _ &amp; workbench Dim inet1 As Inet Dim mypage As String Set inet1 = New Inet With inet1 .Protocol = icHTTP .URL = myURL mypage = .OpenURL(.URL, icString) End With CAMnum = InStr(mypage, "Component Accessory Matrix") intStart = InStrRev(mypage, "pkid=", CAMnum) + 5 newnum = Mid(mypage, intStart, 6) Cells(columnNumber, 2).Value = newnum
    • 哦,抱歉,代码格式不正确。如果你需要格式化的代码,我可以给你发消息。但无论如何,我遇到的问题是 .OpenURL 方法并没有完全保存整个 HTML 文档。它大约有 3,000,000 个字符,但它只保存了大约 100,000 个字符。你知道为什么会这样吗?
    • @user1515191 请更新您的问题,而不是离开 cmets。
    猜你喜欢
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多