2017-04-12 55 views
0
Sub AutoLoadAccounts() 

    Dim IE As Object 
    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Navigate "https://www.urlhere.com/admin/accounts/create" 
    IE.Visible = True 

    While IE.busy 
    DoEvents 
    Wend 

    IE.Document.All("title").Value = ThisWorkbook.Sheets("sheet1").Range("a1") 
    IE.Document.All("names").Value = ThisWorkbook.Sheets("sheet1").Range("b1") 
    IE.Document.All("floor").Value = 30 
    IE.Document.getElementById("status").selectedindex = 1 
    IE.Document.getElementById("email_state").selectedindex = 1 
    IE.Document.All("id").Value = ThisWorkbook.Sheets("sheet1").Range("c1") 
    IE.Document.All("years").Value = ThisWorkbook.Sheets("sheet1").Range("d1") 
    IE.Document.All("submit").Click 

End Sub 

上述代码我用来填充Web表单并提交它。我有大约150行数据,范围从A1:D1。我试图找到一种方法,在提交表单之后循环遍历行1,直到达到结尾。循环通过数据行提交网络表格

所以基本上它会从第一行开始,然后从A1:D1填充字段,然后一次完成到下一行,然后对A2:D2执行相同操作。等等

回答

2

这里的窍门是组织你的源数据。使用两列可以记录字段名称和所需的值:

A   B 
1 Title  Sample Title 
2 Names  Sample Names 
3 Floor  Sample Floor 

要循环:

Sub AutoLoadAccounts() 

    Dim IE As Object 
    Dim cRow As Range  ' Current row, used to extract values from Excel. 

    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Navigate "https://www.urlhere.com/admin/accounts/create" 
    IE.Visible = True 

    While IE.busy 
    DoEvents 
    Wend 

    ' Executes once for each row in the source range. 
    For Each cRow In ThisWorkbook.Sheets("sheet1").Range("A1:A3") 

     ' Read field name and value from current row. 
     IE.Document.All(cRow.Value).Value = cRow.Offset(0, 1) 
    Next 


    IE.Document.All("submit").Click 
End Sub 

这个代码可以改善。此刻源范围是硬编码的(Range("A1:A3"))。您可以改进这一点,以便代码自动识别Excel中所有已完成的行。如果您有兴趣研究工作表UsedRange object

编辑
添加了从列而不是行读取源数据的示例。

Sub AutoLoadAccounts_Columns() 

    Dim IE As Object 
    Dim cRow As Range  ' Current row, used to extract values from Excel. 

    Set IE = CreateObject("InternetExplorer.Application") 
    IE.Navigate "https://www.urlhere.com/admin/accounts/create" 
    IE.Visible = True 

    While IE.busy 
    DoEvents 
    Wend 

    ' Executes once for each row in the source range. 
    For Each cRow In ThisWorkbook.Sheets("sheet1").Range("A1:C1") 

     ' Read field name and value from current row. 
     IE.Document.All(cRow.Value).Value = cRow.Offset(1, 0).Value 
    Next 


    IE.Document.All("submit").Click 
End Sub 
+0

感谢您的回复。 我对你按照你建议的方式组织我的源数据感到有点困惑,因为我没有看到我能如何处理150个不同的项目?将字段名称放在单独的列中是不合理的?例如: A标题B:名称C:楼层,然后在这些列下有所需的数据,因为我有150项数据每个。例如, 。数据将是标题:PSE,名称:杰米:地板:2 - 提交此表格,然后移动到下一行,下一个数据是另一个人完成表格。 – Snowflake232

+0

你可以这样做。如果使用行或列,这并不重要。无论哪种方式,您仍然需要在Excel中完成相同数量的单元格(300 - 150个名称和150个值)。我将添加列版本。 –