如果我调用这样的值,它就会起作用:
=GetElementByRegex(D2;"<a href=""/StatWeb/Field/Index/15"">(.*?)</a>")
但如果我用单元格名称来称呼它,就像这样,它不会:
=GetElementByRegex(D2;L2)
(L2 is "<a href=""/StatWeb/Field/Index/15"">(.*?)</a>")
它返回空值....
或者是否有替代方案我可以用一个单元名称(例如F4)替换索引号(Index / 15)。
我是新人。
我在后面使用的代码就是这个。
Public Function GetElementByRegex(url As String, reg As String)
Dim XMLHTTP As Object, html As Object, objResult As Object
Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = reg
regEx.Global = True
If regEx.Test(XMLHTTP.ResponseText) Then
Set matches = regEx.Execute(XMLHTTP.ResponseText)
GetElementByRegex = matches(0).SubMatches(0)
Exit Function
End If
GetElementByRegex = ""
End Function