x-rates Excel VBA数据拉取
我是Excel VBA的新手,从以下网站提取数据时遇到问题:。我想自动化这个过程,这样我可以得到这些费率每月。这是我所能得到的,我从这里迷路了: '启动名为SearchBot的新子例程x-rates Excel VBA数据拉取,excel,vba,web-scraping,automation,Excel,Vba,Web Scraping,Automation,我是Excel VBA的新手,从以下网站提取数据时遇到问题:。我想自动化这个过程,这样我可以得到这些费率每月。这是我所能得到的,我从这里迷路了: '启动名为SearchBot的新子例程 'dimension (declare or set aside memory for) our variables Dim objIE As InternetExplorer 'special object variable representing the IE browser Dim aEle As HTM
'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link
'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer
'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True
'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "https://www.x-rates.com/table/?from=CAD&amount=1"
子搜索机器人()
”维度(声明或为变量留出内存)
Dim objIE作为InternetExplorer表示IE浏览器的特殊对象变量
Dim aEle作为(链接)元素的HTMLLinkElement特殊对象变量
Dim y As Integer'我们将用作计数器的整数变量
Dim result作为字符串的字符串变量,它将保存我们的结果链接
启动一个新的Internet Explorer实例并将其提交给objIE
Set objIE=新的InternetExplorer
'使IE浏览器可见(False将允许IE在后台运行)
objIE.Visible=True
'导航IE到这个网页(实际上是一个相当整洁的搜索引擎)
objIE.navigate“https://www.x-rates.com/table/?from=CAD&amount=1"
任何帮助都将不胜感激 您可以使用以下命令。它用作一种更快的检索方法。将昨天的日期连接到URL以获取最新的费率。按字母顺序排列的表由其类名和索引位置选择
Option Explicit
Public Sub GetTable()
Dim sResponse As String, html As HTMLDocument, ws As Worksheet, clipboard As Object
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.x-rates.com/historical/?from=CAD&amount=1&date=" & Format$(Date - 1, "yyyy-mm-dd"), False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set html = New HTMLDocument
With html
.body.innerHTML = sResponse
clipboard.SetText .querySelectorAll(".ratesTable").item(1).outerHTML
clipboard.PutInClipboard
End With
ws.Cells(1, 1).PasteSpecial
End Sub
参考资料(VBE>工具>参考资料):
在末尾添加一个
Set ws=Nothing
,Set clipboard=Nothing
,以及Set html=Nothing
。您需要清理这些乱七八糟的东西。@NELMVN据我所知,垃圾收集将处理这些问题,因为对象引用将在过程结束时超出范围。也许我错了。正如@chillin所指出的,当子对象超出范围时,这将被清除。对象变量将释放其引用,运行时调用对象的释放方法以减少对象的引用计数。这与set x=Nothing相同。