Excel Noob将Excel单元格复制到HTML文本框

时间:2014-10-11 20:27:28

标签: excel textbox webpage

我必须每周将500-600个单元格复制到一个文本框中的网站上。我知道如何编码所以我知道如何阅读源代码,但Excel现在已超出我的范围。如何复制Excel单元格以放置在某个文本字段中(我在下面将其分解)。现在我需要帮助它重复它直到它碰到一个空白单元格。

A1 = id='firstname0'
A2 = id='middleinitial0'
A3 = id='lastname0'
A6 = id='ntlogin0'

B1 = id='firstname1'
B2 = id='middleinitial1'
B3 = id='lastname1'
B6 = id='ntlogin1'

C1 = id='firstname2'
C2 = id='middleinitial2'
C3 = id='lastname2'
C6 = id='ntlogin2'

以下是该网站的部分来源:

<td>
    <input type='text' name='ntlogin[]' id='ntlogin0' />
</td>

<td>
    <input type='text' name='firstname[]' id='firstname0' />
</td>

<td class='center'>
    <input type='text' name='middleinitial[]' id='middleinitial0' size='1' />
</td>

<td>
    <input type='text' name='lastname[]' id='lastname0' />
</td>

<td>
    <input type='text' name='hiredate[]' id='hiredate0' />
</td>

该网站询问我要创建多少行。我可以通过1按钮控制租用日期部分。我无法控制网站,所以我无法对其进行任何更改。

1 个答案:

答案 0 :(得分:1)

您可以使用VBA库&#34; Microsoft Internet Controls&#34;和#34; Microsoft HTML对象库&#34;并完全自动化网页互动...

下面是一个宏来执行此操作...要输入宏代码,可以使用 Alt + F11 进入Microsoft Visual Basic for Applications环境。登记/> - 添加库参考(在Tools&gt; References菜单下)
- 添加代码模块(在Insert&gt; Module下)
- 粘贴下面的代码...修改它们的URL

代码将打开IE然后转到网页并等待您点击确定(在您完成所需的任何操作之后......可能登录,在某处导航,输入行数,......)。然后,它将遍历您在电子表格中使用的所有列(使用Find填充numCols)并将单元格值放入具有相关ID的HTML元素中。它不会为你找到 Add Employees ,以防出现问题,但是库确实让你能够.Click()处理事情。

我已经将它用于一些自动化黑客并且看起来非常可靠......困难的部分可能正在等待页面加载/更新 - 但你不应该有这个问题。

'References needed:  "Microsoft Internet Controls" and "Microsoft HTML Object Library"
' (add under menu Tools > References)

Sub populate()

  Dim ws As Worksheet
  Set ws = Application.ActiveSheet

  Dim appIE As InternetExplorer
  Set appIE = New InternetExplorer
  appIE.Visible = True

  appIE.Navigate "http://localhost:8080/your_form"

  If vbCancel = MsgBox("Make sure the page has loaded, enter the number of ids etc. etc. Then click OK", vbOKCancel) Then
    Set appIE = Nothing
    Exit Sub
  End If

  Dim counter As Integer, index As Integer, numCols As Integer
  numCols = ws.Cells.Find(What:="*", After:=[A1], LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
  For counter = 1 To numCols
    'A1 = id='firstname0'
    'A2 = id='middleinitial0'
    'A3 = id='lastname0'
    'A6 = id='ntlogin0'
    index = counter - 1
    appIE.document.getElementById("firstname" & index).Value = ws.Cells(1, counter)
    appIE.document.getElementById("middleinitial" & index).Value = ws.Cells(2, counter)
    appIE.document.getElementById("lastname" & index).Value = ws.Cells(3, counter)
    appIE.document.getElementById("ntlogin" & index).Value = ws.Cells(6, counter)
  Next counter

  Set appIE = Nothing
  Set ws = Nothing

  MsgBox "All done - hit Submit if all OK!"

End Sub

行中价值的版本

'References needed:  "Microsoft Internet Controls" and "Microsoft HTML Object Library"
' (add under menu Tools > References)

Sub populate()

  Dim ws As Worksheet
  Set ws = Application.ActiveSheet

  Dim appIE As InternetExplorer
  Set appIE = New InternetExplorer
  appIE.Visible = True

  appIE.Navigate "http://localhost:8080/your_form"

  If vbCancel = MsgBox("Make sure the page has loaded, enter the number of ids etc. etc. Then click OK", vbOKCancel) Then
    Set appIE = Nothing
    Exit Sub
  End If

  Dim counter As Integer, index As Integer, numRows As Integer
  numRows = ws.Cells.Find(What:="*", After:=[A1], LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  For counter = 1 To numRows
    'A1 = id='firstname0'
    'B1 = id='middleinitial0'
    'C1 = id='lastname0'
    'D1 = id='ntlogin0'
    index = counter - 1
    appIE.document.getElementById("firstname" & index).Value = ws.Cells(counter,1)
    appIE.document.getElementById("middleinitial" & index).Value = ws.Cells(counter,2)
    appIE.document.getElementById("lastname" & index).Value = ws.Cells(counter,3)
    appIE.document.getElementById("ntlogin" & index).Value = ws.Cells(counter,4)
  Next counter

  Set appIE = Nothing
  Set ws = Nothing

  MsgBox "All done - hit Submit if all OK!"

End Sub
相关问题
最新问题