【问题标题】:loop through rows of data to submit web form循环遍历数据行以提交 Web 表单
【发布时间】:2017-04-12 21:19:00
【问题描述】:
    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

我使用上面的代码来填充一个网络表单并提交它。我有大约 150 行数据,范围从 A1:D1。我正在尝试找到一种方法,在提交表单后逐个循环遍历行,直到它到达末尾。

所以本质上它将从第一行开始并填充 A1:D1 中的字段,然后在完成后进入下一行并为 A2:D2 执行相同的操作。等等

【问题讨论】:

    标签: excel vba


    【解决方案1】:

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

        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
    

    【讨论】:

    • 感谢您的回复。我对按照您建议的方式组织我的源数据有点困惑,因为我看不到如何处理这样的 150 个不同的项目?将字段名称放在单独的列中是否有意义?例如:A Title B: Names C: Floor 然后在这些列下有所需的数据,因为我每个都有 150 项数据。例如。数据将是 Title: PSE, Name: Jamie: Floor: 2 - 提交此表单,然后移至下一行,其中下一个数据是另一个人填写表单。
    • 你可以这样做。使用行还是列并不重要。无论哪种方式,您仍然需要在 Excel 中完成相同数量的单元格(300 - 150 个名称和 150 个值)。我将添加列版本。
    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多