使用VBA将来自不同网页的数据导入MS Excel表格中的一个

时间:2014-08-21 12:28:43

标签: excel vba excel-vba

我在MS Excel中使用VBA代码从三个不同的网页导入一些数据。目前,我可以在每个网页的单独Excel工作表中导入数据,并使用另一个VBA进一步将它们连接到一个工作表中。 VBA代码如下:

Sub GetTable()

     Dim ieApp As InternetExplorer
     Dim ieDoc As Object
     Dim ieTable As Object
     Dim clip As DataObject

     'create a new instance of ie
     Set ieApp = New InternetExplorer

     'you don’t need this, but it’s good for debugging
     ieApp.Visible = True

     'assume we’re not logged in and just go directly to the login page
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/LoginAction.do?hmode=loginPage"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     Set ieDoc = ieApp.Document

     'fill in the login form – View Source from your browser to get the control names
     With ieDoc
    .getElementById("userId").setAttribute "value", "rlbdgs"
    .getElementById("userPassword").setAttribute "value", "123"

    '~~> This will select the 2nd radio button as it is `0` based
    .getElementsByName("userType")(1).Checked = True

    .getElementById("hmode").Click
     End With
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     'now that we’re in, go to the page we want
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&lobby=BSL&type=B&startDate=&endDate=&traction=ELEC"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

     'get the table based on the table’s id
     Set ieDoc = ieApp.Document
     Set ieTable = ieDoc.all.Item("report-table")

 'copy the tables html to the clipboard and paste to the sheet
 If Not ieTable Is Nothing Then
    oHTML = ""
    For i = 0 To ieTable.Length - 1
        oHTML = oHTML & ieTable.Item(i).outerHTML
    Next i
    Set clip = New DataObject
    clip.SetText "<html>" & oHTML & "</html>"
    clip.PutInClipboard
    Sheet1.Select
    Sheet1.Range("A1").Select
    Sheet1.PasteSpecial "Unicode Text"
 End If

    'now that we’re in, go to the page we want
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&lobby=AQ&type=B&startDate=&endDate=&traction=ELEC"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    'get the table based on the table’s id
     Set ieDoc = ieApp.Document
     Set ieTable = ieDoc.all.Item("report-table")

 'copy the tables html to the clipboard and paste to the sheet
 If Not ieTable Is Nothing Then
    oHTML = ""
    For i = 0 To ieTable.Length - 1
        oHTML = oHTML & ieTable.Item(i).outerHTML
    Next i
    Set clip = New DataObject
    clip.SetText "<html>" & oHTML & "</html>"
    clip.PutInClipboard
    Sheet2.Select
    Sheet2.Range("A1").Select
    Sheet2.PasteSpecial "Unicode Text"
 End If

    'now that we’re in, go to the page we want
     ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&lobby=KYN&type=B&startDate=&endDate=&traction=ELEC"
     Do While ieApp.Busy: DoEvents: Loop
     Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    'get the table based on the table’s id
     Set ieDoc = ieApp.Document
     Set ieTable = ieDoc.all.Item("report-table")

 'copy the tables html to the clipboard and paste to the sheet
 If Not ieTable Is Nothing Then
    oHTML = ""
    For i = 0 To ieTable.Length - 1
        oHTML = oHTML & ieTable.Item(i).outerHTML
    Next i
    Set clip = New DataObject
    clip.SetText "<html>" & oHTML & "</html>"
    clip.PutInClipboard
    Sheet3.Select
    Sheet3.Range("A1").Select
    Sheet3.PasteSpecial "Unicode Text"
 End If


     'close 'er up
     ieApp.Quit
     Set ieApp = Nothing


 'combine
 Dim J As Integer
On Error Resume Next
Sheets(1).Select
 Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
 Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
 Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
 Next

 End Sub

是否可以修改VBA代码,以便在导入后不使用VBA for combinethe sheet将每个网页的数据导入同一张表格中?

1 个答案:

答案 0 :(得分:2)

是。在第一个if语句中,您有Sheet1。在你的第二个,你有Sheet2。如果将第二个更改为Sheet1,则必须更改范围,以使其不会覆盖工作表中的第一个数据。它可能看起来像这样:

Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"

第二个可能看起来像这样:

Sheet1.Select
Sheet1.Range("A200").Select
Sheet1.PasteSpecial "Unicode Text"

编辑:

在第一个if语句中尝试:

Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
Dim length As Integer
length = selection.rows.count

在第二个和更多if语句中,试试这个:

Sheet1.Select
Sheet1.Range("A" & length + 1).Select
Sheet1.PasteSpecial "Unicode Text"
length = length + selection.rows.count