2014-10-11 102 views
0

我必須每週將500-600單元格複製到文本框中所覆蓋的網站中。我知道如何編寫代碼,所以我知道如何閱讀源代碼,但Excel現在不在我身邊。我如何複製一個Excel單元格放置在某個文本字段中(我已經在下面分解了它)。現在我需要幫助重複它,直到它遇到一個空白單元格。Excel Noob將Excel單元格複製到HTML文本框

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

您可以使用VBA庫「Microsoft Internet控制」和「Microsoft HTML對象庫」和完全自動化網頁交互...

下面是一個宏做...若要進入宏代碼,您可以使用Alt + F11進入Microsoft Visual Basic for Applications環境。
- 添加庫引用(下Tools>References菜單)
- 添加代碼模塊
Insert>Module下) - 下面的代碼粘貼...修改他們的網址

代碼將打開IE然後進入網頁並等待你點擊確定(完成任何你需要的操作後......也許登錄,在某處導航,輸入行數......)。然後,它將遍歷您在電子表格中使用的所有列(使用Find填充numCols),並將單元格值放入具有相關ID的HTML元素中。它不會打到爲你增加員工,以防萬一出錯,但圖書館確實給你的東西.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 

版本VALUES行

'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 
+0

這是一個起點,但我不知道你說的:o我會嘗試一下,但我從來沒有使用Excel – user237025 2014-10-16 22:34:28

+0

發揮@ user237025 - 啊哈!這對新手來說是一個很好的介紹:-)該網站是否公開,您可以分享更多您的示例數據......然後我可以給您一個更完整的示例! – Captain 2014-10-17 06:55:38

+0

不,它是一個內部網站,但我可以給你更多的例子 http://pastebin.com/AaWCZUJe – user237025 2014-10-18 22:57:04

相關問題