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

所以我想我已经接近这个,代码通过一个未定义的地址列,在另一个生成一个谷歌searchurl,然后拉的地址谷歌有一个写它的第三列。

如果我指定单元格的位置,我只能让它工作,我希望它的工作方式是沿列中的每个URL向下写一个地址。

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

不用说,它不工作,我得到IE.Navigate行的自动化错误。

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是正确的,名单是相当长的,所以我想先testing它只有2个地址。

更精通VBA的人能告诉我哪里出错了吗?

更新:更改范围到单元格

包括评论中提出的其他问题,下面在运行时为我工作(即不只是与F8进行时)

 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 

没有冒犯性,但是你的代码(和编码风格)是雷区。

Sheets("Sheet1").Range(r, "A").Value将抛出错误1004, Range不能采取两个参数作为行和列, Cells可以。 将其更改为:工作Sheets("Sheet1").Cells(r, "A").Value

其次,如果Cells(r, "A").Value值为空白/空导航将抛出错误5.在导航之前检查非空值。

Range(r, "C").Value相同的范围问题,错误1004

不写地址:因为你构造不好的内循环,它不会在C列写任何东西。 当满足条件时,在将值写入单元格之前,您将跳出循环。

这里

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

如果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").Range("A" & r).Value

那么当然你需要打印出相同的方式

Range("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 

确实有更多的方法来做你以后的事情,但修理这两件事情。 如果您需要更多的帮助发布您的工作簿。

谢谢

编辑:我对上面的代码做了一些更改。 看起来你很可能已经退出了你的循环。 我能够得到您提供的使用这个工作的url。

你的代码似乎有两个问题。 我没有尝试,但看着你的代码

  1. 范围定义为cell1,cell2。 单元地址是“A2”,“A3”等,所以在你的代码中,你可能需要做Range(“A”&r)来连接行和列。
  2. 即使在此之后,你将会遇到一个问题。 当您导航到一个URL时,您需要等到文档被加载。 你必须做一些事件驱动的代码或循环,直到文档状态准备好或者什么的。 否则,您将无法parsing和读取已加载文档的内容。