我正在尝试使用Excel VBA从A2-D2列中提取信息并将其输入网站,然后单击“下一步”按钮。下面的代码是我到目前为止只能输入第2行的信息。
我希望实现IE打开一个新窗口,在单元格A2到D2中输入值,单击“下一步”按钮,然后循环打开另一个新的IE窗口并输入单元格A3到D3中的值直到它撞到空单元格。
以下是我用于测试的当前数字http://imgur.com/a/88XEF。
提前感谢任何建议。
Sub FillInternetForm()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
IE.Navigate "https://mygift.giftcardmall.com/Card/Login?returnURL=Transactions"
'go to web page listed inside quotes
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
'pause if needed
Application.Wait Now + TimeValue("00:00:02")
IE.Document.All("CardNumber").Value = ThisWorkbook.Sheets("sheet1").Range("a2")
IE.Document.All("ExpirationMonth").Value = ThisWorkbook.Sheets("sheet1").Range("b2")
IE.Document.All("ExpirationYear").Value = ThisWorkbook.Sheets("sheet1").Range("c2")
IE.Document.All("SecurityCode").Value = ThisWorkbook.Sheets("sheet1").Range("d2")
'presses the next button
Set tags = IE.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
End Sub
答案 0 :(得分:1)
您只需要将代码包装在一个循环中。
Dim i as integer
i = 2
do while (ThisWorkbook.Sheets("sheet1").cells(i, 1).value <> "")
'your code from Application.wait line to end of next button click
i = i + 1
loop
这假设A列为空时可以识别空行。如果这个假设不好,你可以改变while循环的条件
答案 1 :(得分:0)
抱歉得到一个新答案,因为格式化很奇怪
确定有意义,你只需要将循环的开始进一步向上移动代码以便它包含IE对象和导航的创建,你还应该在打开一个新窗口之前关闭IE窗口:
移动块:
Dim i as integer
i = 2
do while (ThisWorkbook.Sheets("sheet1").cells(i, 1).value <> "")
直到右上角:
Sub FillInternetForm()
在第&#34; Next&#34;行之后添加以下行:在底部,但仍然被循环包围
IE.Quit
设置IE = Nothing
答案 2 :(得分:0)
我能够通过以下方式完成我想要的任务。感谢那些评论过的人。
Sub FillInternetForm()
Range("A2").Select
Do Until IsEmpty(ActiveCell)
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE. use reference to return current open IE if
'you want to use open IE window. Easiest way I know of is via title bar.
IE.Navigate "https://mygift.giftcardmall.com/Card/Login?returnURL=Transactions"
'go to web page listed inside quotes
IE.Visible = True
While IE.busy
DoEvents 'wait until IE is done loading page.
Wend
'pause if needed
Application.Wait Now + TimeValue("00:00:02")
IE.Document.All("CardNumber").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("ExpirationMonth").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("ExpirationYear").Value = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
IE.Document.All("SecurityCode").Value = ActiveCell.Value
ActiveCell.Offset(1, -3).Select
'presses the next button
Set tags = IE.Document.GetElementsByTagname("Input")
For Each tagx In tags
If tagx.Value = "Next" Then
tagx.Click
Exit For
End If
Next
Loop
End Sub