我正在运行这个Sub并且它正在运行但是数据被写入第二行并且每个实例都覆盖了最后一行。我看不出我的脚本错在哪里。请帮助我理解:
Option Explicit
Sub test()
Dim ele As Object
Dim objIE As Object
Dim RowCount As Double
Dim sht As Worksheet
Dim myjobtype As String
Dim myzip As String
Dim what
Dim zipcode
Set sht = Sheets("Sheet1")
RowCount = 1
sht.Range("A" & RowCount) = "Title"
sht.Range("B" & RowCount) = "Company"
sht.Range("C" & RowCount) = "Location"
sht.Range("D" & RowCount) = "Description"
Set objIE = CreateObject("InternetExplorer.Application")
myjobtype = InputBox("Enter type of job eg. sales, administration")
myzip = InputBox("Enter zipcode of area where you wish to work")
With objIE
.Visible = True
.navigate "http://www.jobs.com/"
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
Set what = .document.getElementsByName("q")
what.Item(0).Value = myjobtype
Set zipcode = .document.getElementsByName("where")
zipcode.Item(0).Value = myzip
.document.forms(0).submit
Do While .Busy Or _
.readyState <> 4
DoEvents
Loop
For Each ele In .document.all
Select Case ele.classname
Case "cardview"
RowCount = RowCount + 1
Case "jobTitle"
sht.Range("A" & RowCount) = ele.innertext
Case "company"
sht.Range("B" & RowCount) = ele.innertext
Case "location"
sht.Range("C" & RowCount) = ele.innertext
Case "preview"
sht.Range("D" & RowCount) = ele.innertext
End Select
Next ele
End With
Set objIE = Nothing
End Sub
答案 0 :(得分:2)
RowCount = RowCount + 1
应移至Select Case
之后,For Each
之后。
For Each ele In .document.all
RowCount = RowCount + 1 '<------------------------------- Here
Select Case ele.classname
Case "cardview"
...
这将为每个元素增加一行,但对于不匹配任何大小写的元素,您将有空行。如果您不想要那些空行,您可以简单地再次递减RowCount
中的Case Else
。但更好的解决方案是简化您的Select
声明,因为对所有情况采取了同样的行动:
For Each ele In .document.all
Select Case ele.classname
Case "cardview", "jobTitle", "company", "location", "preview"
RowCount = RowCount + 1
sht.Range("D" & RowCount) = ele.innertext
End Select
Next ele
答案 1 :(得分:0)
我不确定为什么
Case "cardview"
RowCount = RowCount + 1
未执行。
但是当我搬家时
RowCount = RowCount + 1
低于Case "jobTitle"