VBA在不同的关键短语之前查找短语的最后一个实例? [英] VBA finding the last instance of a phrase before a different key phrase?
问题描述
我有一个来自HTML源代码的大字符串(大约有1,000,000个字符)。我使用msinet.ocx查看适当网站的文本。为了找到恰好在不同关键短语(Component Accessory Matrix)之前出现的关键短语(pkid =),我写了一小段代码,但它不能正常工作。这就是我现在的情况:
workbench =单元格(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,intStart,6)
单元格(columnNumber,2).Value = newnum
这个问题似乎与 mypage = .OpenURL(.URL,icString)
;当我运行 len(mypage)
时,它返回一个大约100,000的值,当它返回一个大约一百万的值时。有人可以解释这一点吗?使用 InStr
和<$ c
c> InStrRev ,找到你的字符串,并向后看。一旦你有了这个位置,从那里往前看,直到找到引号字符。最后,使用这些位置来获取字符串
pos1 = InStrRev(YourXMLString,pkid =,InStr(YourXMLString,Component (Your1),你的XML字符串))+ 5'长度的pkid =
pos2 = InStr(pos1,YourXMLString,Chr(34))'Chr(34)=
FoundString = Mid $(YourXMLString,pos1 ,pos2 - pos1)
请注意,如果Component Accessory Matrix为缺少或没有pkid =之前,或者引号不是分隔符,或者 pkid = 和引号标记之间有更多文本,代码将失败和/或产生无意义的结果。
I have a large string that comes from an HTML source code (approximately 1,000,000 characters long). I'm using msinet.ocx to view the text from appropriate websites. I've written a small segment of code in order to find a key phrase ("pkid=") that occurs right before a different key phrase ("Component Accessory Matrix"), but it's not working properly. Here's what I have right now:
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
The problem seems to be with mypage = .OpenURL(.URL, icString)
; when I run len(mypage)
, it returns a value of approximately 100,000, when it should be returning a value of about a million. Can someone explain this?
using InStr
and InStrRev
, find your string, and look backwards. Once you have that position, look from there forwards until we find the quote character. Finally, use those positions to get the string
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)
Note that if "Component Accessory Matrix" is missing, or doesn't have "pkid=" before it, or the quote is not the delimiter, or more text comes between pkid= and the quote mark, the code will fail and/or produce nonsense results.
这篇关于VBA在不同的关键短语之前查找短语的最后一个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!