Excel VBA中的嵌套循环不工作,因为不同的for循环?

Excel VBA中的嵌套循环不工作,因为不同的for循环?,excel,vba,Excel,Vba,所以我想我已经接近这个了,代码通过一列未定义的地址,在另一列中生成一个谷歌搜索url,然后把谷歌的地址拉到第三列 如果我指定了单元格位置,那么在我只能让它工作之前,我希望它能够工作,方法是遍历列中的每个URL并逐个写入地址 所以我想“让我们把getElementsByClassName放在另一个循环中” 不用说它不起作用,我在IE导航线上遇到了自动化错误 Private Sub CommandButton1_Click() Dim IE As Object ' Create InternetE

所以我想我已经接近这个了,代码通过一列未定义的地址,在另一列中生成一个谷歌搜索url,然后把谷歌的地址拉到第三列

如果我指定了单元格位置,那么在我只能让它工作之前,我希望它能够工作,方法是遍历列中的每个URL并逐个写入地址

所以我想“让我们把getElementsByClassName放在另一个循环中”

不用说它不起作用,我在IE导航线上遇到了自动化错误

Private Sub CommandButton1_Click()
Dim IE As Object

' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")

' You can uncoment Next line To see form results
IE.Visible = False

'START LOOP
' URL to get data from
For r = 2 To 3
    IE.navigate Sheets("Sheet1").Cells(r, "A").Value

    Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop

    Dim dd As String, c
    ' Runs loops to look for the value within the classname, if classname alternates it will change the element, if null it will exit.
        For Each c In Array("vk_sh vk_bk", "_Xbe")
            On Error Resume Next
            dd = IE.document.getElementsByClassName(c)(0).innerText
            On Error GoTo 0
            If Len(dd) > 0 Then Exit For
        ' Gives a confirmation message and writes the result to a cell
        Cells(r, "C").Value = dd
        Next
Next r

' /LOOP

' Show IE
IE.Visible = False

' Clean up
Set IE = Nothing


End Sub
注:

从2到3是正确的,列表很长,所以我想先用2个地址来测试它

是否有更精通VBA的人可以告诉我哪里出了问题


更新:将范围更改为单元格

您的代码似乎有两个问题。我还没有试过,但是看看你的代码

  • 范围定义为cell1、cell2。单元格地址是“A2”、“A3”等,因此在代码中可能需要执行Range(“A”&r)来连接行和列
  • 即使在那之后,你也会有问题。导航到URL时,需要等待文档加载。您必须执行一些事件驱动的代码或循环,直到文档状态就绪或其他。否则,您将无法解析和读取加载文档的内容
    您的代码似乎有两个问题。我还没有试过,但是看看你的代码

  • 范围定义为cell1、cell2。单元格地址是“A2”、“A3”等,因此在代码中可能需要执行Range(“A”&r)来连接行和列
  • 即使在那之后,你也会有问题。导航到URL时,需要等待文档加载。您必须执行一些事件驱动的代码或循环,直到文档状态就绪或其他。否则,您将无法解析和读取加载文档的内容 无意冒犯,但您的代码(和编码风格)是一个雷区

    此部分
    Sheets(“Sheet1”).Range(r,“A”).Value
    将抛出错误1004,
    Range
    不能将两个参数作为行和列,
    单元格可以。将其更改为:
    Sheets(“Sheet1”)。单元格(r,“A”)。值

    其次,如果
    单元格(r,“A”)。值
    为空,则导航将抛出错误5。导航前检查非空值

    同一范围问题,范围(r,“C”)错误1004。值

    未写入地址:由于内部for循环构造不当,它不会在C列中写入任何内容。当满足条件时,在将值写入单元格之前,您将跳出循环

    这里

    如果dd的长度大于0,它将永远不会到达语句,
    ws.Cells(r,“C”).Value=dd

    将其更改为:

     If Len(dd) > 0 Then
            'Gives a confirmation message and writes the result to a cell
             ws.Cells(r,"C").Value= dd
            Exit For
          End If
    
    奖励:学习并开始使用
    F8

    没有冒犯,但您的代码(和编码风格)是一个雷区

    此部分
    Sheets(“Sheet1”).Range(r,“A”).Value
    将抛出错误1004,
    Range
    不能将两个参数作为行和列,
    单元格可以。将其更改为:
    Sheets(“Sheet1”)。单元格(r,“A”)。值

    其次,如果
    单元格(r,“A”)。值
    为空,则导航将抛出错误5。导航前检查非空值

    同一范围问题,范围(r,“C”)错误1004。值

    未写入地址:由于内部for循环构造不当,它不会在C列中写入任何内容。当满足条件时,在将值写入单元格之前,您将跳出循环

    这里

    如果dd的长度大于0,它将永远不会到达语句,
    ws.Cells(r,“C”).Value=dd

    将其更改为:

     If Len(dd) > 0 Then
            'Gives a confirmation message and writes the result to a cell
             ws.Cells(r,"C").Value= dd
            Exit For
          End If
    

    奖励:学习并开始使用
    F8

    一个肯定会给您带来错误的问题是您如何使用范围

    在为代码使用范围时,您需要使用以下内容:

    IE.navigate Sheets(“Sheet1”).范围(“A”&r).值

    当然,你也需要以同样的方式打印出来

    范围(“C”&r).Value=dd

    现在,您可以在代码中添加一些提示以提高效率

    Private Sub CommandButton1_Click()
    
      Dim IE As Object, r as integer
      Dim wb as Workbook, ws as Worksheet
      Dim dd as String, c as Variant, found as Boolean
    
      'Create InternetExplorer Object
      Set IE = CreateObject("InternetExplorer.Application")
      'create other objects
      Set wb = ThisWorkbook
      Set ws = wb.Worksheets("Sheet1")
    
    
      ' You can uncoment Next line To see form results
      IE.Visible = False
    
      'START LOOP
      ' URL to get data from
      For r = 2 To 3
        debug.print ws.Range("A" & r).Value 'see what the url is
        IE.navigate ws.Range("A" & r).Value
        Do while IE.ReadyState <> 4: DoEvents: Loop
        'page loaded.
        ' Runs loops to look for the value within the classname, if classname alternates it will change the element, if null it will exit
        found = false
        For each c in Array("vk_sh vk_bk", "_Xbe")
          On Error Resume Next
          dd = IE.document.getElementsByClassName(c)(0).innerText
          On Error GoTo 0
          If Len(dd) > 0 Then
            found = true
          End If
          If found Then
            ws.Range("C" & r).Value = dd
            dd = ""  'need to set to nothing or it will retain the value.
            Exit For
          End If
        Next c
      Next r
    
      IE.Quit
    
    'Clean up
    Set IE = Nothing
    
    
    End Sub
    
    Private子命令按钮1\u单击()
    Dim IE作为对象,r作为整数
    将wb设置为工作簿,ws设置为工作表
    Dim dd作为字符串,c作为变量,作为布尔值找到
    '创建InternetExplorer对象
    设置IE=CreateObject(“InternetExplorer.Application”)
    '创建其他对象
    设置wb=ThisWorkbook
    设置ws=wb.工作表(“表1”)
    '您可以取消注释下一行以查看表单结果
    可见=假
    '启动循环
    '从中获取数据的URL
    对于r=2到3
    debug.print ws.Range(“A”&r).Value”查看url是什么
    IE.ws.Range(“A”&r).Value
    Do while IE.ReadyState 4:DoEvents:Loop
    '页面已加载。
    '运行循环以查找类名中的值,如果类名交替,它将更改元素,如果为null,它将退出
    发现=错误
    对于数组中的每个c(“vk_sh vk_bk”,“Xbe”)
    出错时继续下一步
    dd=IE.document.GetElementsByCassName(c)(0).innerText
    错误转到0
    如果Len(dd)>0,则
    找到=真
    如果结束
    如果找到了
    ws.Range(“C”&r).Value=dd
    dd=“”需要设置为“无”,否则将保留该值。
    退出
    如果结束
    下一个c
    下一个r
    即退出
    “清理
    设置IE=无
    端接头
    
    肯定有更多的方法来做你想做的事情,但要解决这两件事。如果需要更多帮助,请发布工作簿

    谢谢


    编辑:我对上面的代码做了一些更改。看起来您可能退出循环太快了。我可以使用此工具获取您提供的URL。

    一个肯定会给您带来错误的问题是如何使用范围

    当为代码使用范围时,您将
    Private Sub CommandButton1_Click()
    Dim IE      As Object
    Dim LngRow  As Long
    Dim Wksht   As Worksheet
    Dim dd      As String
    Dim c       As Variant
    
    On Error Resume Next
    
    Set Wksht = ThisWorkbook.Worksheets("Sheet1")
        Set IE = CreateObject("InternetExplorer.Application")
            IE.Visible = False
            'The below will process the all rows in column A
            For LngRow = 2 To Wksht.Range("A" & Wksht.Rows.Count).End(xlUp).Row
                If Wksht.Cells(LngRow, 1) <> "" Then
                    IE.Navigate Wksht.Cells(LngRow, 1)
    
                    'This loops waits for IE to be ready
                    Do Until (IE.Document.ReadyState = "complete") And (Not IE.busy)
                        DoEvents
                    Loop
    
                    For Each c In Array("vk_sh vk_bk", "_Xbe")
                        dd = ""
                        dd = IE.Document.getElementsByClassName(c)(0).innerText
                        If Len(dd) > 0 Then
                            Wksht.Cells(LngRow, 3) = dd
                            Exit For
                        End If
                    Next
    
                End If
            Next
            IE.Quit
        Set IE = Nothing
    Set Wksht = Nothing
    
    End Sub