Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
In My VBA CODE Working in Laptop but not working Desktop in Laptow OS WIN 11 amd Excel 2010. Desktop OS WIN 10 EXCEL 2010. how to Resolve This ERROR.

What I have tried:

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
Posted
Updated 24-Jul-23 8:46am
v2
Comments
Graeme_Grant 24-Jul-23 9:29am    
Typing in ALL CAPS is considered SHOUTING and is rude. PLEASE DO NOT DO IT!

Any code which includes the line "On Error Resume Next" should be taken out and ritually disassembled.

It doesn't "get rid of errors" it hides them until they are too big to ignore - by which point you have probably done significant damage to your database integrity, and lost any and all information about what originally caused the problem.

So nobody can help you: the information about why this occurred has been discarded along with any information about where that happened.

Take out every single occurrence of "On Error Resume Next" from your whole app and run it again in the debugger. When it fails, the debugger will stop and let you look at why it failed. Fix that, and the error you are getting now may also go away ...
 
Share this answer
 
Comments
2011999 24-Jul-23 9:36am    
i am used "On Error Resume Next" when i got this error of Object Required. but not show error, after that code is not executed.
Richard Deeming 24-Jul-23 10:03am    
So as Griff said, rather than dealing with the actual error, you've chosen to ignore all errors and destroy your database instead.

Then, rather than asking us for help to fix the actual error, you've dumped your borked code and demanded a "fix" for the "error", without telling us what the error is.

At a guess, the CreateObject("InternetExplorer.Application") line is failing or returning Nothing, since Internet Explorer is long-dead and no longer installed on new computers.
OriginalGriff 24-Jul-23 10:04am    
As I said: "On Error Resume Next" doesn't fix errors, it hides them and throws away the info you need in order to fix it. Get rid of them all, and never use them again.

You can't fix errors when you don't know "where", "what", and "when" they occur - because that leads to "why" which you need to fix it!
RedDk 24-Jul-23 13:55pm    
"code is not executed" ... so you must see that the URL you've pasted in CPQA is as completely unacceptable to the EXCEL spreadsheet interface as is processed sugar as a sweetener in human food consumption?
As allready mentioned the Code "On Error resume Next" says to the System that the actual Error is to ignore and the System goes automaticly to the next Code-Line.
What you can do now is to code an Error-Handling after each line where an Error could happen. This could look like :
VBA
If Err.Number <> 0 Then
    myMessage = "Error " & CStr(Err.Number) & " " & Err.Description
    ' do something with this info
    Err.Clear
    Exit Sub
End If

This gives you the ability to see where the Error occures and perhaps also what kind of Error it is ...
The Rest is up to you ...
 
Share this answer
 
Comments
Richard Deeming 25-Jul-23 4:58am    
Now I'm getting flashbacks to the horrors of VB (prior to .NET) error handling. :)

At least VBA supports On Error Goto label, unlike VBScript which only had Goto 0 or Resume Next. So rather than checking after every line, you can write a specific error handler section to handle all errors. It's not pretty, but it's better than nothing.
Ralf Meier 25-Jul-23 6:29am    
you might be right - with that I wasn't sure ...
bur nevertheless - the OP could make an Error-Handling which is even better as nothing (like mentioned by you)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900