Excel VBA通​​过IE和循环通过行在线输入数据 [英] Excel VBA to Enter Data Online With IE and Loop Through Rows

查看:272
本文介绍了Excel VBA通​​过IE和循环通过行在线输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Excel VBA从A2-D2列中提取信息并将其输入网站,然后单击下一步按钮。下面的代码是我到目前为止只能输入第2行的信息。

I'm trying to use Excel VBA to pull the info from columns A2-D2 and enter it into the web site and then click the "Next" button. The code below is what I have so far which works fine for entering the info found on row 2 only.

我希望实现IE打开一个新窗口,输入单元格A2到D2中的值,单击下一步按钮,然后循环打开另一个新的IE窗口并输入单元格A3到D3中的值,直到它到达一个空单元格。

I'm hoping to achieve that IE opens a new window, enters the values in cells A2 through D2, clicks the "Next" button, and then loops to open another new IE window and enters the values in cells A3 through D3 until it hits an empty cell.

以下是我用于测试的当前数字, http://imgur.com/ a / 88XEF

Here are the current numbers that I'm using for testing, 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


推荐答案

我能够通过以下方式完成我想要的任务。感谢那些评论过的人。

I was able to accomplish what I wanted with the following. Thanks to those that commented.

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

这篇关于Excel VBA通​​过IE和循环通过行在线输入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆