【问题标题】:Import data from different webpages in one in MS Excel sheet using VBA使用 VBA 在 MS Excel 工作表中从不同网页导入数据
【发布时间】:2014-08-21 12:28:43
【问题描述】:

我在 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 合并工作表,在同一个工作表中导入?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    是的。在您的第一个 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
    

    【讨论】:

    • 也许第二个数据集将被粘贴在从“A200”开始的范围内,这意味着两个数据集之间的空白行。这可能是不可取的。
    • 对。 A200 只是一个例子。您知道每个数据集将占用多少行吗?你也可以让它删除空白行。
    • 行数不固定,因为它取决于特定日期的签核数。
    • 是否有最大条目数?
    • 不,先生。有时会为零,但有时会是 500 或更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2014-10-08
    • 2021-07-23
    • 1970-01-01
    • 2017-05-17
    • 2014-02-13
    相关资源
    最近更新 更多