1

我经常使用一个网站来生成电子表格。网站上仅有的项目是“开始日期”字段、“结束日期”字段和“开始”按钮。在我输入我的日期范围并单击“开始”后,它会下载一个 .cfm 文件,我单击使用 excel 打开,excel 警告该文件具有不同的扩展名并验证它没有损坏,然后我单击打开,我有我需要的数据和来自有一个宏可以根据需要进行操作。我正在寻找自动化步骤:

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

我之前用于从网站获取数据的宏仅复制和粘贴特定 url 上可见的数据,如下所示。我操纵输入电子表格上的 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

谢谢你。

4

1 回答 1

1

以下代码适用于我。您必须根据特定网站命名输入框的方式更改“startDate”和“endDate”。

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
于 2013-10-01T17:41:31.553 回答