1
我曾經使用過google電子表格,特別是它們的importxml()函數。我想知道是否有一種方法可以在VBA中創建函數(對於Excel 2010)(我不是很聰明的在VBA上創建函數),從而可以將google函數複製到excel 2010文件中運行?爲excel2010創建等值的google doc電子表格函數importxml()函數
我發現一些VBA代碼,我認爲它可以使用,但我似乎無法運行它給我我想要的。
理想情況下,我想把一個單元格(在Excel 2010中)以下函數並獲取xpath內容。即:= importXML(「網址」,「xpath」)
任何指導將不勝感激。
最佳 帕科
Option Explicit
Public Sub WaitBrowserQuiet(objIE As InternetExplorer)
Do While objIE.Busy Or objIE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
End Sub
Public Function getXPathElement(sXPath As String, objElement As HTMLBaseElement) As HTMLBaseElement
Dim sXPathArray() As String
Dim sNodeName As String
Dim sNodeNameIndex As String
Dim sRestOfXPath As String
Dim lNodeIndex As Long
Dim lCount As Long
' Split the xpath statement
sXPathArray = Split(sXPath, "/")
sNodeNameIndex = sXPathArray(1)
If Not InStr(sNodeNameIndex, "[") > 0 Then
sNodeName = sNodeNameIndex
lNodeIndex = 1
Else
sXPathArray = Split(sNodeNameIndex, "[")
sNodeName = sXPathArray(0)
lNodeIndex = CLng(Left(sXPathArray(1), Len(sXPathArray(1)) - 1))
End If
sRestOfXPath = Right(sXPath, Len(sXPath) - (Len(sNodeNameIndex) + 1))
Set getXPathElement = Nothing
For lCount = 0 To objElement.childNodes().Length - 1
If UCase(objElement.childNodes().Item(lCount).nodeName) = UCase(sNodeName) Then
If lNodeIndex = 1 Then
If sRestOfXPath = "" Then
Set getXPathElement = objElement.childNodes().Item(lCount)
Else
Set getXPathElement = getXPathElement(sRestOfXPath, objElement.childNodes().Item(lCount))
End If
End If
lNodeIndex = lNodeIndex - 1
End If
Next lCount
End Function
Private Sub cmdGetQuote_Click()
Dim ie As InternetExplorer
Dim elem As HTMLBaseElement
Dim url As String
url = "http://www.bloomberg.com/quote/MXIBTIIE:IND"
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate url
WaitBrowserQuiet ie
Set elem = getXPathElement("//span[@class=' price']", ie.Document)
Range("A1").Value = elem.innerText
Set ie = Nothing
End Sub
您是否試圖提取此信息?道指13,060.10,標準普爾500-1,398.08,納斯達克-3,080.50' – 2012-04-07 13:48:23
沒有。我試圖提取4.76000 – macutan 2012-04-07 19:41:30
嗯,我找不到'4.76000'的價格' – 2012-04-08 08:17:35