需要将我的代码更改为循环(使用VBA将数据填充到HTML)

时间:2017-06-12 20:50:31

标签: excel vba excel-vba loops automation

这是我现在的代码。它适用于单次运行,但我想知道如何将进程更改为循环。

Sub SearchBot()

    Dim objIE As InternetExplorer
    'Dim caseNumber As HTMLLinkElement
    Dim outputa As HTMLLinkElement
    Dim outputb As HTMLLinkElement
    Dim outputc As HTMLLinkElement
    Dim y As Integer
    'Dim outputa As String, outputb As String, outputc As String

    Set objIE = New InternetExplorer

    objIE.Visible = True

    objIE.navigate "https://debbiezhou.github.io/citi-demo/addaddadd.html"

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


    objIE.document.getElementById("fullName").Value = _
      Sheets("Sheet2").Range("A2").Value
    objIE.document.getElementById("nation").Value = _
      Sheets("Sheet2").Range("B2").Value
    objIE.document.getElementById("DOB").Value = _
      Sheets("Sheet2").Range("C2").Value

    objIE.document.getElementById("submit").Click

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    y = 2

    For Each outputa In objIE.document.getElementsByClassName("outName")
    For Each outputb In objIE.document.getElementsByClassName("outNation")
    For Each outputc In objIE.document.getElementsByClassName("outDob")

    Sheets("Sheet2").Range("E" & y).Value = outputa.innerText
    Sheets("Sheet2").Range("F" & y).Value = outputb.innerText
    Sheets("Sheet2").Range("G" & y).Value = outputc.innerText

    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    Debug.Print outputa.innerText
    Debug.Print outputb.innerText
    Debug.Print outputc.innerText
    Next
    Next
    Next
    objIE.Quit
End Sub

我现在使用它的方法是分配每个单元格,但我需要处理大量数据,所以我需要它作为循环或数组。

1 个答案:

答案 0 :(得分:0)

您设置y = 2但不要在循环中递增它 尝试

Sheets("Sheet2").Range("G" & y).Value = outputc.innerText
y=y+1
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop


 objIE.document.getElementById("fullName").Value = _
  Sheets("Sheet2").Range("A" & i).Value
objIE.document.getElementById("nation").Value = _
  Sheets("Sheet2").Range("B" & i).Value
objIE.document.getElementById("DOB").Value = _
  Sheets("Sheet2").Range("C" & i).Value