在网站上更改date和生成电子表格的macros

有一个我经常用来生成电子表格的网站。 网站上的唯一项目是“开始date”字段,“结束date”字段和“开始”button。 当我进入我的date范围,并点击“转到”它下载一个.cfm文件,我点击打开与Excel中,Excel警告该文件有一个不同的扩展名,并validation它没有损坏,我点击打开,我有我需要的数据和有一个macros需要根据需要来制作。 我正在寻找自动化的步骤:

Go to website Change Start Date Change End Date Click Go Click Open file Agree to open different extension 

我以前用来从网站获取数据的macros只复制和粘贴特定url上可见的数据,如下所示。 我操纵我的input电子表格上的url来操纵数据。

 Dim addWS As Worksheet Set addWS = Sheets.Add(Before:=Sheets("Input")) addWS.Name = "Website Data" Dim myurl As String myurl = Worksheets("Input").Range("G4") With Worksheets("Website Data").QueryTables.Add(Connection:= _ "URL;" & myurl, _ Destination:=Range("A3")) .BackgroundQuery = True .TablesOnlyFromHTML = True .Refresh BackgroundQuery:=False .SaveData = True End With 

谢谢。

以下代码适用于我。 你将不得不改变“startDate”和“endDate”取决于具体的网站如何命名input框。

 Sub test_fetch() Dim IE As Object Dim objElement As Object Dim objCollection As Object Dim i As Long Dim Doc As Object, lastrow As Long, tblTR As Object Set IE = CreateObject("InternetExplorer.application") IE.Visible = True IE.navigate "http://your_website" Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop Application.StatusBar = "Fetching Website Data. Please wait..." Set objCollection = IE.document.getElementsByTagName("input") i = 0 While i < objCollection.Length If objCollection(i).Name = "startDate" Then ' Set text for start date objCollection(i).Value = "09/15/2013" ElseIf objCollection(i).Name = "endDate" Then ' Set text for end date objCollection(i).Value = "09/21/2013" Else If objCollection(i).Type = "submit" And _ objCollection(i).Name = "" Then ' "Search" button is found Set objElement = objCollection(i) End If End If i = i + 1 Wend objElement.Click ' click button to search End Sub