Trying to get tables from a piece of code

JudahRaion

New Member
Joined
Oct 13, 2011
Messages
20
Hi everyone,

I need your help to figure how can I take from a piece of code an table to excel.

I find where table is and I create a variable to put the table in, but when I try to use that piece of code like html document, vba shows me this message: "Run-time error '424': Object required".

Code:
Sub Tester()

Dim tbls, tbl, trs, tr, tds, td, r, c
Dim IE As New InternetExplorerMedium


IE.navigate "URL"
IE.Visible = False


Application.Wait Now + TimeSerial(0, 0, 4)


allHTML = IE.document.body.innerHTML


''Carteira
If InStr(allHTML, "Carteira de Títulos") > 0 Then
    leng = Len("Carteira de Títulos")
    carteira = Mid(allHTML, InStr(allHTML, "Carteira de Títulos") + leng, Len(allHTML))
    leng = Len("    carteira = Mid(carteira, InStr(carteira, "<table") +="" leng,="" instr(carteira,="" "<="" table="">") + leng)
    'Range("A3") = carteira
End If

'error shows here
Set tbls = carteira.getElementsByTagName("table")
For r = 0 To tbls.Length - 1
    Debug.Print r, tbls(r).Rows.Length
Next r


Set tbl = carteira.getElementsByTagName("table")(0)
Set trs = tbl.getElementsByTagName("tr")


For r = 0 To trs.Length - 1
    Set tds = trs(r).getElementsByTagName("td")
    'if no  then look for 
    If tds.Length = 0 Then Set tds = trs(r).getElementsByTagName("th")


    For c = 0 To tds.Length - 1
        ActiveSheet.Range("B4").Offset(r, c).Value = tds(c).innerText
    Next c
Next r



End Sub

Thank you all

</table")>
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
carteira is a string, it has no properties.

Why don't you work with the actual HTML document for the page?
Code:
Set tbls = IE.doument.getElementsByTagName("table")
For r = 0 To tbls.Length - 1
    Debug.Print r, tbls(r).Rows.Length
Next r
 
Upvote 0
Why don't you work with the actual HTML document for the page?
Code:
Set tbls = IE.doument.getElementsByTagName("table")
For r = 0 To tbls.Length - 1
    Debug.Print r, tbls(r).Rows.Length
Next r
Hi, I don't use that because it will take all tables from website and I want a specific one.
Thank you for helping me.
 
Upvote 0
What are you trying to do here?
Code:
'error shows here
Set tbls = carteira.getElementsByTagName("table")
For r = 0 To tbls.Length - 1
    Debug.Print r, tbls(r).Rows.Length
Next r

You can't use carteira to access the table elements on the page, it's just a string containing the HTML from the page.
 
Upvote 0
What I'm trying to do is what you're telling me that is not possible...

I want that specific table, but I don't know how can I reach it. The table don't have a ID or name, so I can't go by there... And the only way I found to choose the table is with the string thing.

Can I change that string information to a HTML so I can use it?

Is possible that I can open IE with only that portion of code?

Thank you
 
Upvote 0
I'm not saying it's impossible, I'm saying it's not possible the way you are trying to do it.

Does cartieras contain only the HTML for the table you want?
 
Upvote 0
You can create a new HTML document from an HTML string like this.
Code:
Set doc = New HTMLDocument
doc.body.innerHTML = carteira

You could then use that document to access the table you want.
 
Upvote 0
It worked!! Thank you! :D

Do you know any book with all of VBA information or course? Something.

Thank you again :D
 
Upvote 0

Forum statistics

Threads
1,215,507
Messages
6,125,201
Members
449,214
Latest member
mr_ordinaryboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top