【问题标题】:How to loop a particular code for each populated cell in a column如何为列中的每个填充单元格循环特定代码
【发布时间】:2019-09-03 06:47:26
【问题描述】:

我正在自动执行搜索过程并下载结果。

我的代码运行一行,我想为1st column 中的每个填充单元格循环相同。我尝试了下面的代码,但抛出了一个错误

    Private Sub CommandButton1_Click()
        Dim i As Integer
        Dim IE As New SHDocVw.InternetExplorer
        Dim HTMLDoc As MSHTML.HTMLDocument
        Dim HTMLInput As MSHTML.IHTMLElement
        Dim HTMLAs As MSHTML.IHTMLElementCollection
        Dim HTMLA As MSHTML.IHTMLElement
        Dim Aone As Range
        Dim Bone As Range
        Dim Cone As Range
        Dim SearchButton As Object
        Dim Results As Variant
        For i = 1 To 3
        IE.Visible = False
        IE.navigate "https://www.twinword.com/api/text-similarity.php"



        Do While IE.ReadyState <> READYSTATE_COMPLETE
        Loop

        Set HTMLDoc = IE.Document

        Set Aone = Range("Ai")
        Set Bone = Range("Bi")
        Set Cone = Range("Ci")


        Set HTMLInput = HTMLDoc.getElementById("demoInput1")
        HTMLInput.Value = Aone.Value


        Set HTMLInput = HTMLDoc.getElementById("demoInput2")
        HTMLInput.Value = Bone.Value


        Set SearchButton = HTMLDoc.getElementById("demoButton")

    'click on the first (and presumably only) object in the collection

    SearchButton.Click
    Application.Wait (Now + TimeValue("0:00:02")) '2sec
          Results = HTMLDoc.getElementById("demoBox").innerText
            Range("Ci").Value = Results

    Next i
    End Sub

只想为Column A中的所有单元格运行所需的循环

【问题讨论】:

    标签: excel vba loops for-loop


    【解决方案1】:

    您以错误的方式使用Range 属性。

    更改:

        Set Aone = Range("Ai")
        Set Bone = Range("Bi")
        Set Cone = Range("Ci")
    

    与:

        Set Aone = Range("A" & i)
        Set Bone = Range("B" & i)
        Set Cone = Range("C" & i)
    

    同样改变最后一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多