创建一个宏来填写在线表单,循环设置不正确

时间:2016-03-31 14:05:57

标签: vba

这是代码块的中间部分,我在其中添加了一个循环来访问每个单元格并将其值传递给变量。

我省略了我创建IE对象,访问网站等的代码部分,因为它似乎工作正常。循环是我不确定的。

我需要改变什么?

Dim name As String
Dim address As String
Dim city As String
Dim state As String
Dim zip As String
Dim phone As String

Range.Row = i

    For i = 2 To 626

        name = Cells(A, i).Value
        address = Cells(B, i).Value
        city = Cells(C, i).Value
        state = Cells(D, i).Value
        zip = Cells(E, i).Value
        phone = Cells(F, i).Value

        IE.Document.all("butAdd").Click
        If IE.Document.Title = " PestPac - Maintain Notification Locations" Then
        IE.Document.getElementById("Name").Value = name
        IE.Document.getElementById("Address").Value = address
        IE.Document.getElementById("City").Value = city
        IE.Document.getElementById("State").Value = state
        IE.Document.getElementById("Zip").Value = zip
        IE.Document.getElementById("Phone").Value = phone
        IE.Document.getElementById("Distance").Value = "0.1"

        IE.Document.all("butAdd").Click

        Exit For

    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

您需要在End If之后的If语句末尾添加Exit for语句。如果您希望文档标题是其他内容,则还可以添加Else

https://msdn.microsoft.com/en-us/library/752y8abs.aspx