我有一个代码导航到一个网站,并填写一个33输入的表格。
这是代码:
Dim i As Long
Dim IE As Object
Dim objCollection As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "https://mylink.com"
Do While IE.Busy: DoEvents: Loop
Do While IE.ReadyState <> 4: DoEvents: Loop
Set objCollection = IE.Document.getElementsByTagName("input")
For i = 0 To objCollection.Length
objCollection(i).innertext = "Test " & i
Next i
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing这就像一种魅力。一个错误都没有。
输入1接收“测试1";输入2接收”测试2";.;输入33接收“测试33";
但是,我需要传递的实际数据在我的工作表中,在AI75 43:AI75 75范围内。
如果我改变这部分
For i = 0 To objCollection.Length
objCollection(i).innertext = "Test " & i
Next i到这个
j = 1
For i = 0 To objCollection.Length
objCollection(i).innertext = Range("AI" & 42 + j).Text
j = j + 1
Next i输出,每次都是不同的,而且总是错误的。输入的顺序变得疯狂,一些输入仍然是空白的。
例如:输入1接收“数据1",输入2接收”数据2",输入3接收“数据30",输入4没有接收”数据10“,输入5接收”数据10“。
每次我运行它,输出都是不同的。知道为什么吗?我想不出来。
发布于 2018-01-31 23:15:45
与其使用For i = ...语句,不如通过迭代集合本身来查看For Each语句是否能满足您的需要。
Dim IE As Object, i as Long
Dim objCollection As Object, o As Object ' <--- New declaration
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://mylink.com"
Do While IE.Busy: DoEvents: Loop
Do While IE.readyState <> 4: DoEvents: Loop
Set objCollection = IE.document.getElementsByTagName("input")
For Each o In objCollection
i = i + 1
o.innerText = "Test " & i
Next o
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothinghttps://stackoverflow.com/questions/48551800
复制相似问题