2016-08-16 26 views
0

程序工作正常(所需文本顯示在Excel中)。但之後,IE停止工作和運行時錯誤462(遠程服務器機器不存在或不可用)。在互聯網上搜索解決方案。 https://support.microsoft.com/en-us/kb/178510。 我無法有效更改代碼。將站點中的某些文本複製到Excel(運行時錯誤462)

Sub extractTablesData1() 
Dim IE As Object 
Dim Data As Object 
Dim xlApp As Excel.Application 
Dim xlwb As Excel.Workbook 
Dim xlsh As Excel.Worksheet 
Dim i As Integer 
Dim elemCollection As Variant 

Set xlApp = CreateObject("Excel.Application") 
Set xlwb = xlApp.Workbooks("IESite.xlsx") 
Set xlsh = xlwb.Worksheets("Data") 

Set IE = CreateObject("InternetExplorer.Application") 
With IE 
    .Visible = False 
    .navigate ("http://allscores.ru/soccer/new_ftour.php?champ=2404&f_team=406&tour=110") 
    While IE.ReadyState <> 4 
     DoEvents 
    Wend 
    Set Data = IE.document.getElementsbyTagName("table")(6).querySelectorAll("td.clr, td.clr_win, td.clr_draw, td.clr_loose") 
    i = 1 
    For Each elemCollection In Data 
     xlsh.Cells(34, 1 + i).Value = elemCollection.innerText 
     i = i + 1 
    Next elemCollection 
End With 
IE.Quit 
Set IE = Nothing 

結束子

回答

0

如在鏈接描述:

解決方案
要解決此問題,因此,每個呼叫修改代碼以Excel對象,方法或特性是合格與適當的對象變量。

您正在使用:

ActiveWorkbook.Sheets(1).Cells(34, 1 + i).Value 

這應該是:

Dim xlwb as Workbook 
Dim xlsh as Worksheet 

set xlwb = Workbooks("IESite") 
set xlsh = xlwb.Worksheets("Data") 

xlsh.Cells(34, 1 + i).Value = elemCollection.innerText 

進一步變化:

所要求的解決方案,一切都需要進行定義。讓我知道如果使用下面的方式它確實工作,如果是的話,我會刪除上面的代碼。

不要忘記定義ElemCollection

Sub extractTablesData1() 
    Dim IE As Object 
    Dim Data As Object 
    Dim xlApp As Excel.Application 
    Dim xlwb As Excel.Workbook 
    Dim xlsh As Excel.Worksheet 
    Dim i As Integer 
    Dim elemCollection as ... 'please define elemCollection as the type it is 

    Set xlApp = CreateObject("Excel.Application") 
    Set xlwb = xlApp.Workbooks("IESite.xlsx") 
    Set xlsh = xlwb.Worksheets("Data") 

    Set IE = CreateObject("InternetExplorer.Application") 
    With IE 
     .Visible = False 
     .navigate ("http://allscores.ru/soccer/new_ftour.php?champ=2404&f_team=406&tour=110") 
     While IE.ReadyState <> 4 
      DoEvents 
     Wend 
     Set Data = IE.document.getElementsbyTagName("table")(6).querySelectorAll("td.clr, td.clr_win, td.clr_draw, td.clr_loose") 
     i = 1 
     For Each elemCollection In Data 
      xlsh.Cells(34, 1 + i).Value = elemCollection.innerText 
      i = i + 1 
     Next elemCollection 
    End With 
    IE.Quit 
    Set IE = Nothing 
End Sub 
+0

我改變了代碼,然後我得到運行時錯誤9(下標越界) – maxim465

+0

你改變'Workbook'的命名和「工作表」? – DragonSamu

+0

我改了名字,然後我們回到原來的問題(IE停止工作,運行時錯誤462) – maxim465

相關問題