Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA将不同网页中的数据导入MS Excel工作表中_Vba_Excel - Fatal编程技术网

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

使用VBA将不同网页中的数据导入MS Excel工作表中,vba,excel,Vba,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 ins

我在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
Sub-GetTable()
Dim ieApp作为InternetExplorer
Dim ieDoc作为对象
可作为对象的
将剪辑变暗为数据对象
'创建ie的新实例
设置ieApp=新的InternetExplorer
您不需要这个,但它有助于调试
ieApp.Visible=True
'假设我们没有登录,直接进入登录页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/LoginAction.do?hmode=loginPage"
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
设置ieDoc=ieApp.Document
'填写登录表单–从浏览器查看源代码以获取控件名称
与ieDoc合作
.getElementById(“用户ID”).setAttribute“值”、“rlbdgs”
.getElementById(“用户密码”).setAttribute“值”,“123”
“~~>这将选择第二个单选按钮,因为它基于“0”
.getElementsByName(“用户类型”)(1).Checked=True
.getElementById(“hmode”)。单击
以
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
现在我们进入了,进入我们想要的页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&LOB=BSL&type=B&startDate=&endDate=&traction=ELEC“
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
设置ieTable=ieDoc.all.Item(“报告表”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
oHTML=“”
对于i=0到ieTable,长度为-1
oHTML=oHTML和ieTable.项目(i).外部TML
接下来我
Set clip=新数据对象
clip.SetText“&oHTML&”
夹板
表1.选择
表1.范围(“A1”)。选择
Sheet1.1特殊的“Unicode文本”
如果结束
现在我们进入了,进入我们想要的页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&LOB=AQ&type=B&startDate=&endDate=&traction=ELEC“
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
设置ieTable=ieDoc.all.Item(“报告表”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
oHTML=“”
对于i=0到ieTable,长度为-1
oHTML=oHTML和ieTable.项目(i).外部TML
接下来我
Set clip=新数据对象
clip.SetText“&oHTML&”
夹板
表2.选择
表2.范围(“A1”)。选择
Sheet2.1特殊的“Unicode文本”
如果结束
现在我们进入了,进入我们想要的页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&LOB=KYN&type=B&startDate=&endDate=&traction=ELEC“
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
设置ieTable=ieDoc.all.Item(“报告表”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
oHTML=“”
对于i=0到ieTable,长度为-1
oHTML=oHTML和ieTable.项目(i).外部TML
接下来我
Set clip=新数据对象
clip.SetText“&oHTML&”
夹板
表3.选择
表3.范围(“A1”)。选择
Sheet3.1特殊的“Unicode文本”
如果结束
“关闭”er
退出
设置ieApp=Nothing
“联合收割机
作为整数的Dim J
出错时继续下一步
第(1)页。选择
工作表。添加
第(1)页。Name=“组合”
第(2)页。激活
范围(“A1”).EntireRow.Select
选择。复制目的地:=图纸(1)。范围(“A1”)
对于J=2至张数。计数
第(J)页。激活
范围(“A1”)。选择
Selection.CurrentRegion.Select
Selection.Offset(1,0)。调整大小(Selection.Rows.Count-1)。选择
选择。复制目的地:=页(1)。范围(“A65536”)。结束(xlUp)(2)
下一个
端接头

是否可以修改VBA代码,以便在导入后不使用VBA组合工作表而将每个网页中的数据导入到同一工作表中?

是。在第一个if语句中,您有Sheet1。在你的第二张纸上,你有第二张纸。如果将“第二个”和“第二个”更改为“图纸1”,则必须更改范围,使其不会覆盖图纸中的第一个数据。它可能看起来像这样:

Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
Sheet1.Select
Sheet1.Range("A200").Select
Sheet1.PasteSpecial "Unicode Text"
第二个可能是这样的:

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或更多。