2017-04-12 58 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個值)。我將添加列版本。 –