所以我认为我已经接近这一点,代码通过一列未经定义的地址,在另一个地方生成一个谷歌搜索网址,然后拉取地址谷歌将其写入第三列。
如果我指定了单元格位置之前我才能使它工作,我希望它能够通过记下列中的每个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是正确的,列表很长,所以我想先用2个地址测试一下。
对VBA更熟练的人能否告诉我哪里出错了?
更新:将范围更改为单元格
答案 0 :(得分:1)
没有冒犯,但你的代码(和编码风格)是一个雷区。
此部分Sheets("Sheet1").Range(r, "A").Value
将抛出错误1004,Range
无法将两个参数作为行和列,Cells
可以。将其更改为:Sheets("Sheet1").Cells(r, "A").Value
其次,如果Cells(r, "A").Value
为空/空,导航将抛出错误5.导航前检查非空值。
相同范围问题,Range(r, "C").Value
不写地址:它不会在C列中写任何内容,因为你构造了内部for循环。满足条件时,在将值写入单元格之前,您将跳出循环。
这里
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
答案 1 :(得分:1)
一个肯定会给你错误的问题是你如何使用范围。
为代码使用范围时,您需要使用以下内容:
var hasWhiteboards;
angular.forEach($scope.roomlist, function (list) {
if (list.whiteboards) {
hasWhiteboards = true;
}
});
然后当然你需要以同样的方式打印
IE.navigate Sheets("Sheet1").Range("A" & r).Value
现在提供一些提示,您可以将其放在代码中以提高效率,
Range("C" & r).Value = dd
肯定有更多方法可以完成你所做的事情,但要修复这两件事。如果您需要更多帮助,请发布工作簿。
由于
编辑:我对上面的代码做了一些修改。看起来你可能太快退出了循环。我能够使用此功能获取您提供的URL。答案 2 :(得分:0)
您的代码似乎有2个问题。我没有尝试,但看着你的代码
答案 3 :(得分:0)
包括评论中提出的其他问题,以下在运行时为我工作(即不仅仅是在与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