使用VBA将数据从不同的网页导入MS Excel工作表中

我在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 combinethe表单导入后?

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