通过数据行循环提交网页表单

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。 我试图find一种方法来提交表单之后,循环遍历行1,1,直到它结束。

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

这里的技巧是组织你的源数据。 使用两列可以logging字段名称和所需的值:

  AB 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对象 。

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

 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