【解決方法】EXCEL VBA コード オブジェクトが必要なエラー、OS WINDOWS 10 MS Excel 2010。ただし、GIVE CODE は OS WINDOWS 11 MS Excel 2010 で動作します。

プログラミングQA


私のVBAコードでは、ラップトップでは動作しますが、ラップトップOS WIN 11 amd Excel 2010のデスクトップでは動作しません。デスクトップOS WIN 10 EXCEL 2010。このエラーを解決する方法。

私が試したこと:

im URL As String
    Dim ie As Object ' InternetExplorer
    Dim HTMLDoc As Object ' HTMLDocument
    Dim Table As Object ' HTMLTable
    Dim row As Object ' HTMLTableRow
    Dim cell As Object ' HTMLTableCell
    Dim i As Long, j As Long
    Dim ws As Worksheet
    Dim startRow As Long, startCol As Long
    
    ' Replace this URL with the website URL you want to scrape
    URL = "https://www.xxxxxaaaa.com"

    On Error Resume Next
    ' Replace the worksheet name with the name of the sheet you want to use
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    
    ' Replace the row and column number where you want to start writing the table data
    startRow = 1
    startCol = 1
    
    ' Create a new instance of InternetExplorer and navigate to the URL
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False ' Set to True if you want to see the browser window
    
    ie.navigate URL
    
    ' Wait for the page to load completely
    Do While ie.Busy Or ie.readyState <> 4
        DoEvents
    Loop
    
    ' Get the HTML document
    Set HTMLDoc = ie.document
    
    ' Find the table in the HTML document
    Set Table = HTMLDoc.getElementById("aktuella") ' Replace "table_id" with the actual ID of the table
    
    ' Check if the table exists
    If Not Table Is Nothing Then
        i = startRow
        
        ' Loop through each row in the table
        For Each row In Table.getElementsByTagName("tr")
            j = startCol
            
            ' Loop through each cell in the row
            For Each cell In row.getElementsByTagName("td")
                ' Check if the cell contains a hyperlink
                If cell.getElementsByTagName("a").Length > 0 Then
                    ' If the cell contains a hyperlink, write the hyperlink text and URL to the worksheet
                    ws.Cells(i, j).Value = cell.getElementsByTagName("a")(0).innerText
                    ws.Hyperlinks.Add Anchor:=ws.Cells(i, j), Address:=cell.getElementsByTagName("a")(0).href
                Else
                    ' If the cell does not contain a hyperlink, write the cell value to the worksheet
                    ws.Cells(i, j).Value = cell.innerText
                End If
                
                j = j + 1
            Next cell
            
            i = i + 1
        Next row
    Else
        MsgBox "Table not found on the page."
    End If
    
    ' Clean up
    ie.Quit

解決策 1

「On Error Resume Next」という行を含むコードはすべて取り出して、儀式に従って逆アセンブルする必要があります。

「エラーを取り除く」のではなく、無視できないほど大きくなるまでエラーを隠します。その時点で、おそらくデータベースの整合性に重大な損傷を与え、問題の最初の原因に関するあらゆる情報が失われています。

したがって、誰もあなたを助けることができません。なぜこれが起こったのかに関する情報は、それが起こった場所に関する情報とともに破棄されています。

アプリ全体から「On Error Resume Next」の出現箇所をすべて削除し、デバッガーで再実行します。 失敗するとデバッガーが停止し、失敗した理由を確認できます。 それを修正すると、現在発生しているエラーも消えるかもしれません…

コメント

タイトルとURLをコピーしました